wpf 通过 XAML 向 DataGrid 添加行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18495471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding rows to DataGrid through XAML
提问by dotNET
Is it possible to add one or more rows to WPF DataGrid through XAML, without binding it to a collection. What I'm looking for would essentially be something like:
是否可以通过 XAML 向 WPF DataGrid 添加一行或多行,而不将其绑定到集合。我正在寻找的东西基本上是这样的:
<DataGrid AutoGenerateColumns="False">
<DataGrid.Columns>
...
</DataGrid.Columns>
<DataGrid.Items>
<DataGridRow>
...
</DataGridRow>
</DataGrid.Items>
</DataGrid>
I'm going to use it at design-time to see how my DataGrid columns would look like without actually running the code.
我将在设计时使用它来查看我的 DataGrid 列在不实际运行代码的情况下会是什么样子。
回答by dotNET
Feeling lucky. Found it myself. Here's the simplest way.
感觉很幸运。自己找的。这是最简单的方法。
Create a dummy class with the same public properties (important that you define members as properties and not fields). For example:
创建一个具有相同公共属性的虚拟类(重要的是您将成员定义为属性而不是字段)。例如:
public class Dummy
{
public string Subject { get; set; }
public string Body { get; set; }
public DateTime DueDateStart { get; set; }
}
Import your project namespace into XAML by adding the following import at the top:
通过在顶部添加以下导入,将您的项目命名空间导入 XAML:
xmlns:local="clr-namespace:YourProjectNamespace"
Now you can add items (rows) to your DataGrid at design-time like (make sure your columns have proper bindings):
现在您可以在设计时将项目(行)添加到您的 DataGrid 中,例如(确保您的列具有正确的绑定):
<DataGrid AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Subject" Binding="{Binding Path=Subject}"/>
<DataGridTextColumn Header="Body" Binding="{Binding Path=Body}"/>
<DataGridTextColumn Header="Due Date" Binding="{Binding Path=DueDateStart}"/>
</DataGrid.Columns>
<local:Dummy Subject="Subject 1" Body="Body 1" ... />
<local:Dummy Subject="Subject 2" Body="Body 2" ... />
</DataGrid>
Hope this helps someone!
希望这对某人有帮助!
Update
更新
Since this is now a popular post, I thought I should update this with the standardway of doing things.
由于这现在是一个流行的帖子,我想我应该用标准的做事方式来更新它。
WPF supports a concept known as Design-Time Data that serves this exact purpose. A few key advantages of using Design-Time Data over the approach I mentioned above include:
WPF 支持一种称为“设计时数据”的概念,该概念可满足此确切目的。与我上面提到的方法相比,使用设计时数据的几个主要优势包括:
- Design-Time Data remains separate from application functionality.
- You do not need to change anything at all to switch your controls between design and run modes.
- Data lives in XML files that are easily editable.
- 设计时数据与应用程序功能保持分离。
- 您根本不需要更改任何内容即可在设计和运行模式之间切换控件。
- 数据存在于易于编辑的 XML 文件中。
Here are the steps to create design-time data files:
以下是创建设计时数据文件的步骤:
- Open your project in Blend (comes free with VS2015 and VS2017).
- Open your View (Window or Control that you're working with).
- From Data tool window (docked with Solution Explorer by default), choose Create Sample Data From Class.
- Select your VM class. You should choose the same class that your control will actually use at runtime as its
DataContext. Blend will create an XML file with sample data automatically filled in for you. The file will look something like this:
<local:TestDataList xmlns:local="clr-namespace:YourNamespaceHere" Capacity="46" ID="33" Name="Maecenas curabitur cras"> <local:TestData ID="66" Name="Aenean vestibulum class"/> <local:TestData ID="34" Name="Duis adipiscing nunc praesent"/> <local:TestData ID="91" Name="Accumsan bibendum nam"/> </local:TestDataList>
- 在 Blend 中打开您的项目(VS2015 和 VS2017 免费提供)。
- 打开您的视图(您正在使用的窗口或控件)。
- 从数据工具窗口(默认情况下与解决方案资源管理器对接),选择Create Sample Data From Class。
- 选择您的 VM 类。您应该选择控件在运行时实际使用的相同类作为它的
DataContext. Blend 将创建一个 XML 文件,其中会自动为您填充示例数据。该文件将如下所示:
<local:TestDataList xmlns:local="clr-namespace:YourNamespaceHere" Capacity="46" ID="33" Name="Maecenas curabitur cras"> <local:TestData ID="66" Name="Aenean vestibulum class"/> <local:TestData ID="34" Name="Duis adipiscing nunc praesent"/> <local:TestData ID="91" Name="Accumsan bibendum nam"/> </local:TestDataList>
Important to note that you do not need Blend to generate this file. You can do this by hand too.
需要注意的是,您不需要 Blend 来生成此文件。您也可以手动执行此操作。
Now in your DataGrid (or whatever control you're working with), add the following property (change file path according to your project):
d:DataContext="{d:DesignData Source=SampleData/TestDataListSampleData.xaml}"- Given that your control has its properties properly set (e.g.
ItemsSource,Columnsetc.), sample data will start showing in the designer immediately.
现在在您的 DataGrid(或您正在使用的任何控件)中,添加以下属性(根据您的项目更改文件路径):
d:DataContext="{d:DesignData Source=SampleData/TestDataListSampleData.xaml}"- 鉴于您的控件已正确设置其属性(例如
ItemsSource,Columns等),示例数据将立即开始在设计器中显示。
Just for completion, note that Blend cannot generate automatic data for generic classes. For example, if your VM class contains a property of type List<string>(or if the VM class itself is a generic class), you'll not see that property getting generated in the sample data file. In that case, you must create your own dummy class inheriting from the generic class and then use it as the type of your property. For example:
只是为了完成,请注意 Blend 无法为泛型类生成自动数据。例如,如果您的 VM 类包含一个 type 属性List<string>(或者如果 VM 类本身是一个泛型类),您将不会在示例数据文件中看到该属性生成。在这种情况下,您必须创建自己的从泛型类继承的虚拟类,然后将其用作属性的类型。例如:
public class MyListOfStrings : List<string>
{ }

