如何在 WPF 中动态创建数据网格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16120010/
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
How to dynamically create a datagrid in WPF?
提问by JSchwartz
I have the following datagrid in XAML:
我在 XAML 中有以下数据网格:
<DataGrid ItemsSource="{Binding View}" AutoGenerateColumns="False" IsReadOnly="True"
GridLinesVisibility="None" CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeColumns="False" CanUserResizeRows="False"
CanUserReorderColumns="False" >
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="12" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Type" Width="200" FontSize="12"
Binding="{Binding Path=Name}" />
<DataGridTemplateColumn Header="Ingredients" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Ingredients}"
AutoGenerateColumns="False" IsReadOnly="True"
GridLinesVisibility="None" CanUserAddRows="False"
CanUserDeleteRows="False" CanUserResizeColumns="False"
CanUserResizeRows="False" CanUserReorderColumns="False" >
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="12" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Ingredients"
Width="*" FontSize="12"
Binding="{Binding Path=IngredientName}"/>
<DataGridTextColumn Header="Quantite" Width="*"
FontSize="12" Binding="{Binding Path=Qty}"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I am trying to find a way to create the datagrid dynamically (in-code) so that I can create multiple copies of it and bind it to different datasources at run-time.
我试图找到一种方法来动态(代码中)创建数据网格,以便我可以创建它的多个副本并在运行时将其绑定到不同的数据源。
Is this possible? Anyone know how I could go about it for a datagrid complicated like this?
这可能吗?任何人都知道我该如何处理像这样复杂的数据网格?
回答by Sphinxxx
First, move as much as possible of the different settings out into reusable Stylesand DataTemplates, leaving very little in the DataGrid itself:
首先,将尽可能多的不同设置移到可重用的Styles和 中DataTemplates,在 DataGrid 本身中留下很少的内容:
<UserControl ... >
<UserControl.Resources>
<Style x:Key="GridHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="12" />
</Style>
<Style x:Key="ReadOnlyGridStyle" TargetType="{x:Type DataGrid}" >
<Setter Property="AutoGenerateColumns" Value="False" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="GridLinesVisibility" Value="None" />
<Setter Property="CanUserAddRows" Value="False" />
<Setter Property="CanUserDeleteRows" Value="False" />
<Setter Property="CanUserResizeColumns" Value="False" />
<Setter Property="CanUserResizeRows" Value="False" />
<Setter Property="CanUserReorderColumns" Value="False" />
<Setter Property="ColumnHeaderStyle" Value="{StaticResource GridHeaderStyle}" />
</Style>
<DataTemplate x:Key="IngredientsCellTemplate" >
<DataGrid ItemsSource="{Binding Ingredients}"
Style="{StaticResource ReadOnlyGridStyle}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Ingredients" Width="*" FontSize="12"
Binding="{Binding Path=IngredientName}" />
<DataGridTextColumn Header="Quantite" Width="*" FontSize="12"
Binding="{Binding Path=Qty}" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</UserControl.Resources>
<!-- A DataGrid using our Styles: -->
<DataGrid ItemsSource="{Binding View}"
Style="{StaticResource ReadOnlyGridStyle}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Type" Width="200" FontSize="12"
Binding="{Binding Path=Name}" />
<DataGridTemplateColumn Header="Ingredients" Width="*"
CellTemplate="{StaticResource IngredientsCellTemplate}" />
</DataGrid.Columns>
</DataGrid>
</UserControl>
Then it gets a lot easier to create new DataGrids in your code-behind, using the existing Styles:
然后使用现有样式在代码隐藏中创建新的 DataGrid 变得容易得多:
var datagrid = new DataGrid();
datagrid.Style = FindResource("ReadOnlyGridStyle") as Style;
datagrid.Columns.Add(new DataGridTextColumn()
{
Header = "Type",
Width = new DataGridLength(200),
FontSize = 12,
Binding = new Binding("Name")
});
datagrid.Columns.Add(new DataGridTemplateColumn()
{
Header = "Ingredients",
Width = new DataGridLength(1, DataGridLengthUnitType.Star),
CellTemplate = FindResource("IngredientsCellTemplate") as DataTemplate
});
datagrid.ItemsSource = ...

