Wpf 将字符串列表绑定到DataGridTemplateColumn';组合框
这个问题可能看起来很简单,但我找不到,我试了所有我找到的东西。它适用于类列表,因为我可以绑定类的一个属性,但不适用于简单的可观察的字符串集合 以下是我在Xaml中的代码:Wpf 将字符串列表绑定到DataGridTemplateColumn';组合框,wpf,combobox,observablecollection,datagridtemplatecolumn,celleditingtemplate,Wpf,Combobox,Observablecollection,Datagridtemplatecolumn,Celleditingtemplate,这个问题可能看起来很简单,但我找不到,我试了所有我找到的东西。它适用于类列表,因为我可以绑定类的一个属性,但不适用于简单的可观察的字符串集合 以下是我在Xaml中的代码: <DataGridTemplateColumn x:Name="VT" Header="VT" Width="*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboB
<DataGridTemplateColumn x:Name="VT" Header="VT" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding ListOfString}"
SelectedValuePath="ListOfString"
DisplayMemberPath="ListOfString"
ItemsSource="{Binding RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type UserControl}},
Path=DataContext.ListOfString}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我的代码隐藏工作正常,因为当我打开下拉列表时,它显示的行数与其中的元素数相同,但行数为空。我也尝试过使用CellEditingTemplate之类的东西,但在最好的情况下,效果是一样的。我认为我在那些DisplayMemberPath属性和其他属性上出错了,但我不知道应该从中得到什么
那么,如何将ObservableCollection正确绑定到ComboBox
谢谢 如果您的列表字符串如下所示:
public ObservableCollection<string> ListOfString {get;set;}
public-observeCollection-ListOfString{get;set;}
然后只需删除SelectedValuePath和DisplayMemberPath。否则,wpf将查找字符串上不存在的属性ListOfString:)
如果不是这样,您应该发布ListOfString属性的代码
顺便说一句,您应该重命名SelectedItem属性或collection属性,因为atm都命名为ListOfString。Oh wow!信息技术作品非常感谢你们这些盲人。很简单,真的,但现在我明白了。我没有发布我的OC代码,因为它与您的代码类似,带有get和set,不用担心,这个名称只是对我的问题的测试;)它可以工作,但当我在单元格中更改焦点时,它不会显示我的值。。。我想这是另一个问题。
<ComboBox SelectedItem="{Binding ListOfString}"
ItemsSource="{Binding RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type UserControl}},
Path=DataContext.ListOfString}"/>