如何在 WPF 中动态创建数据网格? [英] How to dynamically create a datagrid in WPF?

查看:17
本文介绍了如何在 WPF 中动态创建数据网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 XAML 中有以下数据网格:

I have the following datagrid in 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?

推荐答案

首先,将尽可能多的不同设置移到可重用的StylesDataTemplates 中,留下DataGrid 本身很少:

First, move as much as possible of the different settings out into reusable Styles and DataTemplates, leaving very little in the DataGrid itself:

<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 变得容易得多:

Then it gets a lot easier to create new DataGrids in your code-behind, using the existing Styles:

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 = ...

这篇关于如何在 WPF 中动态创建数据网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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