简体   繁体   中英

WPF Design Time Data not binding when class has default constructor

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?

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; }
    }

}

设计时间数据的屏幕截图

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM