WPF - 将字符串列表绑定到数据网格中的列 [英] WPF - Binding a list of a string to a column in Data Grid
问题描述
我有一个带有三列的数据网格。
I have a datagrid with three columns.
<DataGrid IemSource={Binding SomeData}>
<DataGrid.Columns>
<DataGridTemplateColumn>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<ItemsControl ItemSource="{Binding SomeList}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="SomeTopic"/>
<ComboBox ItemSource="{Binding }"/>
</StackPanel>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
SomeData是一个对象集合ClassA。 A类包含两个字符串字段,并将它们绑定到前2列。
SomeData is a object collection of ClassA. Class A contains two string fields and I have binded them to the first 2 columns.
该类包含名称为SomeList的字符串变量列表。我绑定到第3列。我需要将它附加到列3的标题中的组合框中。但是这个代码并不能给我所需要的。
The class contains a List of string variables of which the name is SomeList. I have binded it to the 3rd column. I need to attach it to the combobox in the header of column 3. But this code does not give me what I want.
有人可以帮助吗? b $ b
can someone help ?
推荐答案
您的问题似乎是将StackPanel作为Item添加到ItemsControl中,而不是在DataTemplate中使用它。
Your problem seems to be that you add the StackPanel as item to the ItemsControl instead of using it in a DataTemplate.
而不是
<ItemsControl ItemSource="{Binding SomeList}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="SomeTopic"/>
<ComboBox ItemSource="{Binding }"/>
</StackPanel>
</ItemsControl>
应该如下所示:
<ItemsControl ItemSource="{Binding SomeList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="SomeTopic"/>
<ComboBox ItemSource="{Binding }"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这篇关于WPF - 将字符串列表绑定到数据网格中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!