gpt4 book ai didi

c# - 如何在 DataGrid WPF 中显示 SortedDictionary?

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:29 25 4
gpt4 key购买 nike

我想在 dataGrid 中显示一个 sortedDictionary,当键为 int 且值为 ClassType 列表时,例如 value -人员的列表

例如:

public MainWindow()
{
InitializeComponent();
SortedDictionary<int, List<Person>> Sd1 = new SortedDictionary<int,List<Person>>();
Person p1 = new Person("htryh");
Person p2 = new Person("juyik");
List<Person> PL = new List<Person>();
PL.Add(p1);
PL.Add(p2);
Sd1.Add(1, PL);
dt1.ItemsSource = Sd1;

}
}
class Person
{
public string Name { get; set; }
public Person(string name)
{
Name = name;
}
}

dataGrid 的“Key”列中,我看到了 int,但在“value”列中,我看到了:(收藏)。我该如何解决这个问题??谢谢。

最佳答案

在 dataGrid 上将 AutoGenerateColumns 设置为 False 并提供您自己的一组列。

<DataGrid x:Name="dt1" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Key" Binding="{Binding Key}"/>
<DataGridTemplateColumn Header="Value">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Value}"
IsSynchronizedWithCurrentItem="True"
DisplayMemberPath="Name"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

关于c# - 如何在 DataGrid WPF 中显示 SortedDictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25228390/

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