具有自定义类的 C# wpf 中的数据网格

本文关键字:数据 数据网 网格 wpf 自定义 | 更新日期: 2023-09-27 18:34:34

我在 C# 中有一个看起来像这样的类:

class Program
{
   string name;
   float number;
   string type;
   Value[] parameters=new Value[40];
}
class Value
{
   float parameter;
   string parameter name;
}

另外,我有一个DataGrid应该是这样的:

前 3 列分别是 NumberNameType 列。其他是 40 个参数名称Value[index].parameterName).

DataGrid中的每一行应该代表一个Program对象,并显示 3 个属性值numbernametype,后跟Value[index].parameter的值。

Values大小是动态分配的,我宁愿主要在 C# 代码中而不是在 xaml 中执行此操作。另外,我需要更改单元格中的值(数字除外(。

是否有我可以为此实现的事件?

谢谢!

具有自定义类的 C# wpf 中的数据网格

首先,我想建议你应该有值类列表而不是数组(因为你的大小不是预先知道的(像

class Program
{
   string name;
   float number;
   string type;
   List<Value> parameters;
}

现在,您应该在DataGrid中创建DataGridTemplateColumn以显示动态大小参数,例如

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Program}">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ListBox ItemsSource="{Binding parameters}">
                          <ListBox.ItemsPanel>
                              <ItemsPanelTemplate>
                                  <StackPanel IsItemsHost="True" Orientation="Horizontal"/>
                              </ItemsPanelTemplate>
                          </ListBox.ItemsPanel>
                        </ListBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>