Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当类具有默认构造函数时,WPF设计时数据不绑定_C#_Wpf_Xaml_Data Binding - Fatal编程技术网

C# 当类具有默认构造函数时,WPF设计时数据不绑定

C# 当类具有默认构造函数时,WPF设计时数据不绑定,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我试图在WPF中绑定设计时数据。我有一个视图模型,我的设计时数据类继承自视图模型。我在类的构造函数中填充我希望在设计时看到的数据,如下所示 public class SalesModelDesignTimeData : SalesModel { public SalesModelDesignTimeData() { Items.Add(new SaleItem { Sku = "001", Title = "Pepsi", CostPrice = 10.0, Q

我试图在WPF中绑定设计时数据。我有一个视图模型,我的设计时数据类继承自视图模型。我在类的构造函数中填充我希望在设计时看到的数据,如下所示

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 });
    }
}
这是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>

我面临的问题是VS在我设置d:DataContext的XAML文件中给了我一个“对象引用未设置为对象错误的实例”

如果我从我的类中删除构造函数,那么错误就会消失,并且它似乎可以工作

有人能告诉我我做错了什么吗?

没必要说那些废话

xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
正如用户在评论中解释的那样

这将起作用,是前进的方向

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

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

...
//在您评论他仍然看不到设计时数据后完成代码

main window.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; }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfDataControls
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
}
公共类SalesModelDesignTimeData:SalesModel
{
public SalesModelDesignTimeData()
{
添加(新SaleItem{Sku=“001”,Title=“Pepsi”,CostPrice=10.0,Quantity=1});
添加(新销售项目{Sku=“002”,Title=“可口可乐”,成本价格=10.0,数量=1});
添加(新销售项目{Sku=“003”,Title=“高露洁牙膏”,成本价格=8.0,数量=1});
添加(新的SaleItem{Sku=“004”,Title=“Lipton Yello标签”,成本价格=12.5,数量=1});
添加(新销售项目{Sku=“005”,Title=“Sugar”,成本价格=5.0,数量=1});
}
}
公共类销售模式
{
公共列表项{get;set;}
公共销售模型()
{
项目=新列表();
}
}
公营物品
{
公共字符串Sku{get;set;}
公共字符串标题{get;set;}
公共双成本价格{get;set;}
公共整数数量{get;set;}
}
}

事实证明,我发布的代码工作正常

这个链接帮助我解决了这个问题


我的构造函数正在创建对象链,其中一个是抛出异常。一旦我解决了这个问题,设计时绑定就开始工作了。

d
是一个名称空间,但是您可以访问并分配这个
d:DataContext
,这很奇怪。左侧应该是property、dependencyProperty或field,但所有这些成员都属于类或对象。那么,
DataContext
(在
d:DataContext
中使用)到底是什么呢?它肯定不是属性、dependencyProperty或字段。@无可救药,我还是WPF的新手,所以可能我犯了一个错误,但在线阅读文章这似乎是绑定设计时数据的方法,如您的链接中所述看起来像
DataContext
是一个属性,但我从未使用过类似的东西,这是某种编辑器的惯例/语言,而不是访问元素的正确逻辑。如果
DataContext
是一个属性,那么
d:DataContext
仍然是一个类的访问,因此看到这个
d:DataContext=…
有点混乱和没有意义。也许这是为XAML中的某个元素设置属性的一种方法。可能是重复的,我删除了d名称空间并像您提到的那样绑定了数据,但它仍然给我相同的错误“对象引用未设置为对象的实例”。我认为您需要包含上述名称空间,因为我只想在设计时绑定数据,而不想在运行时绑定数据?如果您在xaml中定义静态资源或datacontext,它在设计时可用。我在检查了答案的准确性后发布了答案。我将发布相同的截图。等等,很好用!谢谢Anjum!你的方法更简单更干净。标记为答案。你说“不需要那个废话”是什么意思?这就是使用设计时数据的方式。您的建议似乎是简单地将数据上下文设置为设计时数据,然后在运行时应用该数据。您最好在designinstance上使用d:DataContext和bind,正如这里所解释的那样,您不会因为不必要地创建Designtime类的实例而污染正在运行的应用程序。我试图使设计时数据显示的方法也可行,但Anjum下面的答案更清晰,效果非常好,因此我接受了他的答案,并将使用该技术绑定设计时数据。