WPF TwoWay将一些元素绑定到ObservableCollection
问题描述:
我需要将一些ComboBoxes绑定到一个ObservableCollection。 我有这个ListView
。WPF TwoWay将一些元素绑定到ObservableCollection
<ListView x:Name="lwCoefTables" Grid.Column="1" ItemsSource="{Binding Source={StaticResource CollectionCoefContainers}}">
<ListView.ItemTemplate>
<DataTemplate>
<ComboBox x:Name="cmbCoefTableTypes" ItemsSource="{Binding Source={StaticResource CollectionCoefLinksTable}}"
SelectedItem="{Binding CoefLinksTableType, Mode=TwoWay}" Grid.Column="1" VerticalAlignment="Center"
HorizontalAlignment="Left" Width="180" DisplayMemberPath="Name">
</ComboBox>
</DataTemplate>
</ListView.ItemTemplate>
我希望我收集绑定到所有ComboBox,并保存每个ComboBox中选定的项目。 如果我填一个集并将其绑定到双向模式下,所有的组合框,我得到这样的:
我想我需要帮助类将包含一些类似的集合。怎么做?
答
所以我假设CoefLinksTableType
属性是在CollectionCoefContainers
里面的项目?
在这种情况下,这应该工作,除非你有相同的实例重复CollectionCoefContainers
内。
例如
这样的事情会像你描述的那样行事。
var vm = new VM();
CollectionCoefContainers.Add(vm);
CollectionCoefContainers.Add(vm);
CollectionCoefContainers.Add(vm);
CollectionCoefContainers.Add(vm);
解决办法是
CollectionCoefContainers.Add(new VM());
CollectionCoefContainers.Add(new VM());
CollectionCoefContainers.Add(new VM());
CollectionCoefContainers.Add(new VM());
这可能是有用的,有你的CollectionCoefContainers
和CollectionCoefLinksTable
定义谢谢。有用! – algreat