WPF双向绑定一些元素到ObservableCollection

3

我需要将一些ComboBox绑定到一个ObservableCollection上。 我有这个ListView

<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>

我想将我的集合绑定到所有的组合框并保存每个组合框的选定项目。如果我填写一个集合并将其以双向模式绑定到所有组合框上,则会得到如下结果:

图片

我认为我需要一个辅助类,其中包含一些类似的集合。如何做到这一点?

1个回答

3

我猜想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());

可能会有用的是您对CollectionCoefContainersCollectionCoefLinksTable的定义。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接