通过 XAML 向 DataGrid 添加行 [英] Adding rows to DataGrid through XAML

查看:35
本文介绍了通过 XAML 向 DataGrid 添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过 XAML 向 WPF DataGrid 添加一行或多行,而无需将其绑定到集合.我正在寻找的内容基本上是这样的:

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:

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>
    ...
    </DataGrid.Columns>

    <DataGrid.Items>
        <DataGridRow>
        ...
        </DataGridRow>
    </DataGrid.Items>
</DataGrid>

我将在设计时使用它来查看我的 DataGrid 列在不实际运行代码的情况下会是什么样子.

I'm going to use it at design-time to see how my DataGrid columns would look like without actually running the code.

推荐答案

感觉很幸运.自己找的.这是最简单的方法.

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

通过在顶部添加以下导入将您的项目命名空间导入 XAML:

Import your project namespace into XAML by adding the following import at the top:

xmlns:local="clr-namespace:YourProjectNamespace"

现在您可以在设计时将项目(行)添加到您的 DataGrid 中,例如(确保您的列具有正确的绑定):

Now you can add items (rows) to your DataGrid at design-time like (make sure your columns have proper bindings):

<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!

由于这现在是一个受欢迎的帖子,我想我应该用标准的做事方式来更新它.

Since this is now a popular post, I thought I should update this with the standard way of doing things.

WPF 支持一种称为设计时数据"的概念,该概念可满足此确切目的.与我上面提到的方法相比,使用设计时数据的几个主要优势包括:

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:

  1. 设计时数据与应用功能保持分离.
  2. 您根本不需要更改任何内容即可在设计和运行模式之间切换控件.
  3. 数据存在于易于编辑的 XML 文件中.

以下是创建设计时数据文件的步骤:

Here are the steps to create design-time data files:

  1. 在 Blend 中打开您的项目(VS2015 和 VS2017 免费提供).
  2. 打开您的视图(您正在使用的窗口或控件).
  3. 从数据工具窗口(默认与解决方案资源管理器对接),选择从类创建示例数据.
  4. 选择您的 VM 类.您应该选择控件在运行时实际使用的类作为其 DataContext.
  5. Blend 将创建一个 XML 文件,其中会自动为您填充示例数据.该文件将如下所示:

  1. Open your project in Blend (comes free with VS2015 and VS2017).
  2. Open your View (Window or Control that you're working with).
  3. From Data tool window (docked with Solution Explorer by default), choose Create Sample Data From Class.
  4. Select your VM class. You should choose the same class that your control will actually use at runtime as its DataContext.
  5. 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 来生成此文件.您也可以手动执行此操作.

  1. 现在在您的 DataGrid(或您正在使用的任何控件)中,添加以下属性(根据您的项目更改文件路径):

  1. 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}"

  • 鉴于您的控件已正确设置其属性(例如 ItemsSourceColumns 等),示例数据将立即开始在设计器中显示.
  • Given that your control has its properties properly set (e.g. ItemsSource, Columns etc.), sample data will start showing in the designer immediately.
  • 只是为了完成,请注意 Blend 无法为泛型类生成自动数据.例如,如果您的 VM 类包含 List 类型的属性(或者如果 VM 类本身是一个通用类),您将不会在示例数据文件中看到该属性生成.在这种情况下,您必须创建自己的从泛型类继承的虚拟类,然后将其用作属性的类型.例如:

    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:

    public class MyListOfStrings : List<string>
    { }
    

    这篇关于通过 XAML 向 DataGrid 添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆