动态构建嵌套的 WPF DataGrid
我在这个网站和其他网站上找到了很多代码,这些代码展示了如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在代码隐藏中创建 DataGrid 只需执行以下操作:
可以在代码隐藏中添加 DataGrid 列,请参阅以下示例 来自 MSDN:
代码隐藏中最棘手的部分可能是创建行模板。代码中 DataTemplate 的构造已在其他问题中介绍:
在代码后面创建 DataTemplate
Creating a DataGrid in code-behind is just a matter of doing the following:
The DataGrid columns can be added in code-behind, see the following example from MSDN:
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