.net 如何使用DataGrid动态生成单元格/列?

6yoyoihd  于 2023-10-21  发布在  .NET
关注(0)|答案(1)|浏览(94)
public class PrcDeployModel : BindableBase
{
    public ProcessComputerResult ProcessComputer { get; set; }

    public List<EnvironmentModel> Environments { get; set; }
}

public class EnvironmentModel
{
    public string Environment { get; set; }

    public string Date { get; set; }

    public string User { get; set; }

    public DeployModel LatestDeployment { get; set; }
}

上面你看到的是一类有着不同环境的计算机。我现在想在DataGrid中根据这些计算机的列表生成列。我可以用后面的代码成功地创建列标题。我想生成一个表,其中1列为计算机名称,然后根据不同的环境使用不同的列。

public void FillTableColumns(List<PrcDeployModel> deployModels)
{
    DataGridDeployments.Columns.Add(new DataGridTextColumn() { Header = "PRC" });

    foreach (var model in deployModels)
    {
        foreach (var environment in model.Environments)
        {
            var existingColumn = DataGridDeployments.Columns.FirstOrDefault(x =>
                environment.Environment != null && (string)x.Header == environment.Environment);
            if (existingColumn == null)
            {
                DataGridDeployments.Columns.Add(new DataGridTemplateColumn() { Header = environment.Environment, IsReadOnly = true, Width = DataGridLength.Auto });
            }
        }
    }
}

我也知道我可以从后面的代码中生成单元格并对它们进行样式化,但我认为这是矫枉过正。我想以某种方式使用绑定并为环境列填充此DataTemplate

<DataTemplate x:Key="TestDataTemplate">
    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding Path=IconSrc}" Width="60" Height="60" Margin="0 0 5 0"/>
        </StackPanel>
        <StackPanel Orientation="Vertical">
            <TextBlock Text="{Binding Path=Environment}" FontWeight="Bold"/>
            <TextBlock Text="{Binding Path=Date}" FontSize="14"/>
            <TextBlock Text="{Binding Path=User}" FontSize="14"/>
        </StackPanel>
    </StackPanel>
</DataTemplate>

或者解决动态列问题的最佳方法是什么?
这是我的table:环境的数量取决于PrcDeployModels列表中不同环境的数量:

ckx4rj1h

ckx4rj1h1#

对于“绑定部分”,而不是后面的代码,您可以使用嵌套的ItemsControl而不是DataGrid。在这里,你去与一个初始的解决方案,以获得一个想法。

<ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding ProcessComputer}"/>
                    <ItemsControl Grid.Column="1" ItemsSource="{Binding Path=Environments}" >
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="{Binding Path=Environment}" FontWeight="Bold"/>
                                <TextBlock Text="{Binding Path=Date}" FontSize="14"/>
                                <TextBlock Text="{Binding Path=User}" FontSize="14"/>
                            </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

创建示例数据的代码:

List<PrcDeployModel> data = new List<PrcDeployModel>();

        for (int i = 0; i < 10; i++)
        {
            PrcDeployModel computer = new PrcDeployModel();

            computer.ProcessComputer = i.ToString();
            computer.Environments = new List<EnvironmentModel>();
            for (int j = 0; j < 4; j++)
            {
                EnvironmentModel env = new EnvironmentModel();
                env.Environment = $"env{j}";
                env.Date = DateTime.Now.ToString();
                env.User = "my user";
                computer.Environments.Add(env);
            }
            data.Add(computer);
        }

        this.DataContext = data;

但是,您的数据似乎非常非结构化。并非所有环境都必须存在于每台机器上,并且数量是无限的。如果它真的那么复杂,我宁愿引入一个类Environment并基于它绑定列。重新考虑你的数据模型

相关问题