ListViews的ItemsControl,绑定每个ListView的SelectedItem,其中ListViews的数量是动态的,MVVM [英] ItemsControl of ListViews, bind SelectedItem from each ListView where the amount of ListViews are dynamic, MVVM
问题描述
我有一个内部具有ListViews的ItemsControl,以创建自定义网格,如布局
。代码大致类似于此操作,更改了名称并删除了不相关的样式。
I have an ItemsControl with ListViews inside to make a custom grid like layout Here is roughly what the code looks like to do this, names changed and irrelevant styling is removed.
每个ListView是由ItemsSubList的一个实例创建的,因此没有要设置的固定数量的列表框或属性。
Each ListView is created by an instance of ItemsSubList and therefore there is no set amount of listboxes or properties to be made.
有人知道我如何将能够以某种方式将每个列表框的SelectedItem属性绑定到一个列表中。
Does anyone know How I would be able to bind the SelectedItem property of each listbox out somehow, into a list preferably.
<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>
推荐答案
我会在您的模型中解决它。
您的数据上下文似乎是一个类,其属性为 Items
类型为 ObservableCollection< MyItem>
的类 MyItem
,然后将其作为属性 ItemsSubList
,类型为 ObservableCollection< MySubItem>
。
I would solve it in your Model.
Your datacontext seems to be a class with a property Items
of type ObservableCollection<MyItem>
and your class MyItem
then as a property ItemsSubList
of type ObservableCollection<MySubItem>
. Is that right?
ListView的datacontext是MyItem类型的实例。您可以将属性 SelectedSubItem
添加到类 MyItem
并使用双向绑定将其绑定到列表框。
The datacontext of your ListView is the instance of type MyItem. You can add a Property SelectedSubItem
to your class MyItem
and bind it with Two-way-binding to your listbox.SelectedItem.
然后在您的主类中,该类保存您的模型以及属性 Items
的住处,在那里添加属性
public List< MySubItem> SelectedSubItems => Items.Select(i => i.SelectedSubItem).ToList();
,该属性可从所有项目中动态选择所有选定的子项目。
And then in your main class, that holds your model and where the property Items
lives, there I would add a property
public List<MySubItem> SelectedSubItems => Items.Select(i => i.SelectedSubItem).ToList();
, a property that dynamically selects all selected subitems from all your items.
这篇关于ListViews的ItemsControl,绑定每个ListView的SelectedItem,其中ListViews的数量是动态的,MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!