gpt4 book ai didi

c# - 如何在 WPF 中动态创建数据网格?

转载 作者:行者123 更新时间:2023-11-30 14:12:51 25 4
gpt4 key购买 nike

我在 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>

我正在尝试找到一种动态(在代码中)创建数据网格的方法,以便我可以创建它的多个副本并在运行时将它绑定(bind)到不同的数据源。

这可能吗?任何人都知道如何处理像这样复杂的数据网格?

最佳答案

首先,将尽可能多的不同设置移出到可重用的 StylesDataTemplates 中,在 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>

然后使用现有样式在您的代码隐藏中创建新的 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 = ...

关于c# - 如何在 WPF 中动态创建数据网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16120010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com