WPF TwoWay将一些元素绑定到ObservableCollection

时间:2011-10-06 14:05:29

标签: wpf binding observablecollection two-way-binding

我需要将一些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>

我希望将我的集合绑定到所有ComboBox并为每个ComboBox保存选定的项目。 如果我填写一个集合并将其绑定到TwoWay模式中的所有组合框,我得到这个:

Picture

我认为我需要包含一些类似集合的辅助类。怎么做?

1 个答案:

答案 0 :(得分:3)

所以我假设CoefLinksTableType内的CollectionCoefContainers属性是<{1}}?

在这种情况下,这应该可行,除非您在CollectionCoefContainers内重复了相同的实例。

e.g。

这样的事情就像你描述的那样。

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

可能会有用
相关问题