【问题标题】:WPF TwoWay Binding some elements to ObservableCollectionWPF TwoWay 将一些元素绑定到 ObservableCollection
【发布时间】:2011-10-06 14:05:28
【问题描述】:

我需要将一些 ComboBoxes 绑定到一个 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

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

【问题讨论】:

    标签: wpf binding observablecollection two-way-binding


    【解决方案1】:

    所以我假设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 可能会很有用

    【讨论】:

      猜你喜欢
      • 2018-05-14
      • 2023-04-10
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 2013-04-18
      相关资源
      最近更新 更多