1

I am trying to bind design time data in WPF. I have a viewModel and my design time data class inherits from View Model. I am populating the data that I want to see at design time in constructor of the class like so

public class SalesModelDesignTimeData : SalesModel
{
    public SalesModelDesignTimeData()
    {

        Items.Add(new SaleItem { Sku = "001", Title = "Pepsi", CostPrice = 10.0, Quantity = 1 });
        Items.Add(new SaleItem { Sku = "002", Title = "Coca Cola", CostPrice = 10.0, Quantity = 1 });
        Items.Add(new SaleItem { Sku = "003", Title = "Colgate Tooth Paste", CostPrice = 8.0, Quantity = 1 });
        Items.Add(new SaleItem { Sku = "004", Title = "Lipton Yello Label", CostPrice = 12.5, Quantity = 1 });
        Items.Add(new SaleItem { Sku = "005", Title = "Sugar", CostPrice = 5.0, Quantity = 1 });
    }
}

Here is the XAML

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:designData="clr-namespace:Tienda.UI.DesignTimeData"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" x:Class="Tienda.UI.Views.Sales"
    Title="Sales" d:DesignWidth="775"
d:DataContext="{d:DesignInstance designData:SalesModelDesignTimeData,IsDesignTimeCreatable=True}">
<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>

    <DataGrid HorizontalAlignment="Left" DataContext="{Binding Items}"/>
    <TextBlock Text="{Binding Test}" Grid.Row="0" Grid.Column="1" />
</Grid>

The problem that I am facing is VS is giving me an "Object Reference not set to instance of an object error" in my XAML file where I am setting the d:DataContext

If I remove the constructor from my class then the error goes away and it binding seems to work.

Can someone please tell me what am I doing wrong?

Afraz Ali
  • 2,672
  • 2
  • 27
  • 47
  • `d` is a namespace but you access and assign this `d:DataContext`, which is strange. The left side should be property, dependencyProperty or field but all those members belong to ***class*** or object. So what exactly is `DataContext` (used in `d:DataContext`) here? It is surely not a property, dependencyProperty or field. – Hopeless Oct 04 '15 at 08:25
  • @Hopeless I am still new to WPF so maybe I am making a mistake but reading articles on-line this seems to be the way to bind design time data like mentioned here https://msdn.microsoft.com/en-us/magazine/dn169081.aspx – Afraz Ali Oct 04 '15 at 14:29
  • from your link looks like `DataContext` is an attribute but still I've never used something like that, it's some kind of the editor's convention/language rather than the correct logic of accessing elements. If `DataContext` is an attribute `d:DataContext` is still an access to a class, so it's a bit confusing and non-sense to see this `d:DataContext=...`. Maybe it's a way to set Attribute for some element in XAML. – Hopeless Oct 04 '15 at 15:28
  • Possible duplicate of [How to see design-time data-binding in XAML editor (it works in runtime)?](https://stackoverflow.com/questions/16401885/how-to-see-design-time-data-binding-in-xaml-editor-it-works-in-runtime) – Sebastian May 25 '18 at 08:06

2 Answers2

1

No need for that crap

xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

as explained by user hopeless in the comment.

This will work and is the way to go.

<Window.DataContext>
            <designData:SalesModelDesignTimeData />
</Window.DataContext>
...

<DataGrid HorizontalAlignment="Left" ItemsSource="{Binding Items}"/>

// Complete code after you commented that he still can't see design time data

MainWindow.xaml

<Window x:Class="WpfDataControls.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfDataControls"
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <local:SalesModelDesignTimeData />
    </Window.DataContext>

    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>

        <DataGrid HorizontalAlignment="Left" ItemsSource="{Binding Items}"/>
        <TextBlock Text="{Binding Test}" Grid.Row="0" Grid.Column="1" />
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfDataControls
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();            
        }
    }

    public class SalesModelDesignTimeData : SalesModel
    {
        public SalesModelDesignTimeData()
        {
            Items.Add(new SaleItem { Sku = "001", Title = "Pepsi", CostPrice = 10.0, Quantity = 1 });
            Items.Add(new SaleItem { Sku = "002", Title = "Coca Cola", CostPrice = 10.0, Quantity = 1 });
            Items.Add(new SaleItem { Sku = "003", Title = "Colgate Tooth Paste", CostPrice = 8.0, Quantity = 1 });
            Items.Add(new SaleItem { Sku = "004", Title = "Lipton Yello Label", CostPrice = 12.5, Quantity = 1 });
            Items.Add(new SaleItem { Sku = "005", Title = "Sugar", CostPrice = 5.0, Quantity = 1 });
        }
    }

    public class SalesModel
    {
        public List<SaleItem> Items { get; set; }

        public SalesModel()
        {
            Items = new List<SaleItem>();
        }
    }

    public class SaleItem
    {
        public string Sku { get; set; }
        public string Title { get; set; }
        public double CostPrice { get; set; }
        public int Quantity { get; set; }
    }

}

Screen shot of design time data

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • I removed the d namespace and bounded the data like you mentioned but it is still giving me the same error "Object reference not set to instance of an object". I was of the view that you need to include the above namespace because I want to bind the data only in design time and not at run time? – Afraz Ali Oct 04 '15 at 14:24
  • If you define a static resource or a datacontext in xaml, it is available at design time. I post answer after checking its accuracy. I will post screenshot of the same. wait. – AnjumSKhan Oct 04 '15 at 14:35
  • Works perfectly! Thanks Anjum! your way is simpler and more cleaner. Marked as answer. – Afraz Ali Oct 04 '15 at 15:11
  • What do you mean "No need for that crap"? That's how you use design-time data. What you seem to be advising is to simply set the data context to the design-time data, which will also then apply at run-time. – Adam Goodwin Dec 29 '17 at 11:56
  • You should better use d:DataContext and bind on designinstance, like explained here https://stackoverflow.com/a/16402388/3111930 then you don't pollute your running Application with unnecessary creation of an instance of your Designtime Class. – Sebastian May 25 '18 at 08:04
1

As it turns out the code that I posted works just fine.

This link helped me to fix the issue.

WPF/MVVM setting UserControl.DataContext in XAML results in Object reference not set?

My constructor was creating chain of objects, one of which was throwing exception. Once I fixed that problem, the design time binding started to work.

Community
  • 1
  • 1
Afraz Ali
  • 2,672
  • 2
  • 27
  • 47
  • The way I was trying to make design time data appear works also but Anjum's answer below is much cleaner and works perfectly so I accepted his answer and will use that technique to bind design time data. – Afraz Ali Oct 04 '15 at 15:12