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?