gpt4 book ai didi

wpf - 具有可变列数的 Infragistics XamDataGrid

转载 作者:行者123 更新时间:2023-12-04 18:29:10 27 4
gpt4 key购买 nike

我需要能够支持 XamDataGrid,它在设计时不会有一定数量的列。例如,应用程序将运行,从服务器获取一些数据并创建一些对象。根据服务器的响应,每次运行应用程序时我可能有不同数量的对象。

这是我的意思的一个例子。假设我调用了一些服务并返回一个包含一些信息的 xml 响应。我将该响应反序列化为多个对象,每次调用时这些对象都可能不同。

假设每个对象都有两个属性,标签和值。我希望网格显示带有标签的列,这些标签与 Label 的值与 Value 的值相匹配。所以如果我有两个对象,obj1 和 obj2,看起来像这样:

obj1.Label = "Parts"
obj1.Value = "17"

obj2.Label = "Parts"
obj2.Value = "12"

我想要一个看起来像这样的网格,有两行:

零件

17

12

如果我将我的数据源绑定(bind)到网格,网格会自动使用对象的属性来创建列,所以我看到标签和值的列:

标签值

第 17 部分

第 12 部分

我假设我无法仅通过 xaml 实现我想要的。有没有人有我正在寻找的例子?在运行时以编程方式创建所有列是否由我决定?

最佳答案

 <Grid>
<DataGrid Name="dgTest" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=ItemsSource[0].Label}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>

和代码:
public partial class Window12 : Window
{
public Window12()
{
InitializeComponent();

List<MyClass> l = new List<MyClass>();

l.Add(new MyClass
{
Label = "Parts",
Value = "17"
});

l.Add(new MyClass
{
Label = "Parts",
Value = "12"
});

dgTest.ItemsSource = l;
}
}

public class MyClass
{
public string Label { get; set; }
public string Value { get; set; }
}

关于wpf - 具有可变列数的 Infragistics XamDataGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5784440/

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