【问题标题】:ItemsControl of ListViews, bind SelectedItem from each ListView where the amount of ListViews are dynamic, MVVMListViews的ItemsControl,从每个ListViews中绑定SelectedItem,其中ListViews的数量是动态的,MVVM
【发布时间】:2018-10-23 08:58:50
【问题描述】:

我有一个 ItemsControl 里面有 ListViews 来制作一个像布局一样的自定义网格 以下是执行此操作的大致代码,更改了名称并删除了不相关的样式。

每个 ListView 都是由 ItemsSubList 的一个实例创建的,因此没有设定数量的列表框或要创建的属性。

有谁知道我如何能够以某种方式将每个列表框的 SelectedItem 属性绑定到一个列表中。

<ItemsControl ItemsSource="{Binding Items}">
  <ItemsControl.ItemsPanel>
     <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"/>
     </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>

  <ItemsControl.ItemTemplate>
     <DataTemplate>
         <ListView ItemsSource="{Binding ItemsSubList}" 
                              DisplayMemberPath="ItemVersion" 
                              SelectionMode="Single"
                              Width="100"
                              VerticalAlignment="Top"
                              Background="#282828"
                              BorderBrush="#282828">
         </ListView>
     </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

【问题讨论】:

    标签: c# wpf xaml mvvm data-binding


    【解决方案1】:

    我会在你的模型中解决它。 您的数据上下文似乎是一个具有ObservableCollection&lt;MyItem&gt; 类型的属性Items 和您的类MyItem 然后作为ItemsSubList 类型ObservableCollection&lt;MySubItem&gt; 的属性的类。对吗?

    ListView 的数据上下文是 MyItem 类型的实例。您可以将属性 SelectedSubItem 添加到您的类 MyItem 并使用双向绑定将其绑定到您的 listbox.SelectedItem。

    然后在你的主类中,它包含你的模型和属性Items 所在的位置,我会在那里添加一个属性 public List&lt;MySubItem&gt; SelectedSubItems =&gt; Items.Select(i =&gt; i.SelectedSubItem).ToList();,一个从所有项目中动态选择所有选定子项目的属性。

    【讨论】:

    • 非常适合我的解决方案。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 2011-07-30
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多