动态构建嵌套的 WPF DataGrid

发布于 2024-12-03 12:54:04 字数 1657 浏览 0 评论 0原文

我在这个网站和其他网站上找到了很多代码,这些代码展示了如何在 XAML 中创建嵌套数据网格,但我无法找到有关如何使用 C# 代码获得相同结果的任何信息。我希望能够将其转换

<DataGrid ...>
  <DataGrid.Columns>
    <DataGridTextColumn Header="Received Date" Binding="{Binding Received}" .../>
     ...
  </DataGrid.Columns>
  <DataGrid.RowDetailsTemplate>
     <DataTemplate>
        <DataGrid ItemsSource="{Binding Details}" ...>
           <DataGrid.Columns>
              <DataGridTextColumn Header="Log Date" Binding="{Binding LogDate}" />
              ...
           </DataGrid.Columns>
         </DataGrid>
      </DataTemplate>
    </DataGrid.RowDetailsTemplate>
  <DataGrid>

为 C# 代码。

数据结构是这样的:

public class MessageData {
   Guid MessageId {get; set;}
   DateTime ReceivedDate { get; set; }
   ...
   List<EventDetail> Details { get; set; }
}

public class EventDetail {
   Guid MessageId { get; set; }
   DateTime LogDate { get; set; }
   string LogEvent { get; set; }
   ...
}

现在看来,除了能够定义内部数据网格中的列之外,我可以让大部分工作正常工作 - 如果我将自动生成设置为 true,则代码可以工作,但我无法弄清楚如何定义内部网格的列。

DataGrid dg = new DataGrid();
...
dg.IsReadOnly = true;
FrameworkElementFactory details = FrameworkElementFactory(typeof(DataGrid));
details.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("Details"));
details.SetValue(DataGrid.AutoGenerateColumnsProperty, true);
DataTemplate dt = new DataTemplate(typeof(DataGrid));
dt.VisualTree = details;
dt.RowDetailsTemplate = dt;
dg.ItemsSource = myDataSouce;

这有效,但是当我将 AutoGenerateColumns 设置为 false - 并尝试定义它失败的列时......

I've found lots of code on this site and others that show how to create a nested datagrid in XAML but I'm unable to locate any information on how to use c# code to obtain the same results. What I would like to be able to do is convert this:

<DataGrid ...>
  <DataGrid.Columns>
    <DataGridTextColumn Header="Received Date" Binding="{Binding Received}" .../>
     ...
  </DataGrid.Columns>
  <DataGrid.RowDetailsTemplate>
     <DataTemplate>
        <DataGrid ItemsSource="{Binding Details}" ...>
           <DataGrid.Columns>
              <DataGridTextColumn Header="Log Date" Binding="{Binding LogDate}" />
              ...
           </DataGrid.Columns>
         </DataGrid>
      </DataTemplate>
    </DataGrid.RowDetailsTemplate>
  <DataGrid>

to C# code.

The data structure is something like this:

public class MessageData {
   Guid MessageId {get; set;}
   DateTime ReceivedDate { get; set; }
   ...
   List<EventDetail> Details { get; set; }
}

public class EventDetail {
   Guid MessageId { get; set; }
   DateTime LogDate { get; set; }
   string LogEvent { get; set; }
   ...
}

It seems that now I can get most of it working except being able to define the columns in the inner datagrid - the code works if I set auto generate to true but I am unable to figure out how to define the columns for the inner grid.

DataGrid dg = new DataGrid();
...
dg.IsReadOnly = true;
FrameworkElementFactory details = FrameworkElementFactory(typeof(DataGrid));
details.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("Details"));
details.SetValue(DataGrid.AutoGenerateColumnsProperty, true);
DataTemplate dt = new DataTemplate(typeof(DataGrid));
dt.VisualTree = details;
dt.RowDetailsTemplate = dt;
dg.ItemsSource = myDataSouce;

this works but when I set AutoGenerateColumns to false - and try to define the columns it fails...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抹茶夏天i‖ 2024-12-10 12:54:04

在代码隐藏中创建 DataGrid 只需执行以下操作:

var dataGrid = new DataGrid();

可以在代码隐藏中添加 DataGrid 列,请参阅以下示例 来自 MSDN

//Create a new column to add to the DataGrid
DataGridTextColumn textcol = new DataGridTextColumn();
//Create a Binding object to define the path to the DataGrid.ItemsSource property
//The column inherits its DataContext from the DataGrid, so you don't set the source
Binding b = new Binding("LastName");
//Set the properties on the new column
textcol.Binding = b;
textcol.Header = "Last Name";
//Add the column to the DataGrid
DG2.Columns.Add(textcol);

代码隐藏中最棘手的部分可能是创建行模板。代码中 DataTemplate 的构造已在其他问题中介绍:

在代码后面创建 DataTemplate

Creating a DataGrid in code-behind is just a matter of doing the following:

var dataGrid = new DataGrid();

The DataGrid columns can be added in code-behind, see the following example from MSDN:

//Create a new column to add to the DataGrid
DataGridTextColumn textcol = new DataGridTextColumn();
//Create a Binding object to define the path to the DataGrid.ItemsSource property
//The column inherits its DataContext from the DataGrid, so you don't set the source
Binding b = new Binding("LastName");
//Set the properties on the new column
textcol.Binding = b;
textcol.Header = "Last Name";
//Add the column to the DataGrid
DG2.Columns.Add(textcol);

Probably the trickiest part to do in code-behind is create your row template. The construction of DataTemplates in code has been covered in other questions:

Create DataTemplate in code behind

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文