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.
有人可以帮忙吗?
推荐答案
您的问题似乎是您将 StackPanel 作为项目添加到 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屋!