如何链接两个列表框以选择/取消选择和激活命令 [英] How to link two listboxes to select/deselect and fire command

查看:103
本文介绍了如何链接两个列表框以选择/取消选择和激活命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的情景:



两个列表框 - Listbox_01 Listbox_02

Listbox_01 ItemsSource绑定到ViewModel中的ObservableCollection,SelectionMode设置为Multiple,附加属性将选择限制为2个项目。 SelectedItems(依赖属性)绑定到ViewModel上的属性(IList)

Listbox_02 ItemsSource绑定到ViewModel上的属性(IList)(来自Listbox_01的SelectedItems)。 SelectionMode设置为Single。

当我从Listbox_01中选择项目时,所有项目都应出现在Listbox_02中。这适合我。问题是,如何链接这两个列表框以选择和取消选择它们之间的项目

如果我从Listbox_01中选择两个项目,它们将在Listbox_02中可见。然后,当我从Listbox_02中选择项目时,它应该获得所选项目(列或内容),激活命令(RelayCommand),在Listbox_01上取消选择该项目并从Listbox_02中消失。



这里的问题是,当我点击Listbox_02项目时,Listbox_01上始终有选择,而且从Listbox_02中消失的项目不正确(当我有两个项目时,我点击item_02关闭它,item_01消失)



Listbox_01仅用于选择项目。 Listbox_02是执行命令。



我的代码:

Listbox_01

< pre lang =XML> < listv:ListViewSelectedItemBehavior SelectionMode = Multiple

< span class =code-attribute> < span class =code-attribute> ItemsSource = {Binding EmployeeViewM.MainActivity}

< span class =code-attribute> < span class =code-attribute> SelectedItemsList = {Binding EmployeeViewM.SelectedAll,Mode = OneWayToSource}

< span class =code-attribute> 背景 = {x:Null}

BorderBrush = {x:Null} < span class =code-attribute>

< span class =code-attribute> IsSynchronizedWithCurrentItem = False >





Listbox_02

 <   listv:ListViewSelectedItemBehavior     SelectionMode   = 单个  

< span class =code-attribute> Horizo​​ntalAlignment =

< span class =code-attribute> < span class =code-attribute> ItemsSource = {Binding EmployeeViewM.SelectedAll}

< span class =code-attribute> < span class =code-attribute> SelectedItemsList = {Binding EmployeeViewM.SelectedAll,Mode = TwoWay}

背景 = {x:Null}

< span class =code-attribute> BorderBrush = {x:Null}

< span class =code-attribute> IsSynchronizedWithCurrentItem = False >





SelectedAll IList



 公共 属性 SelectedAll()作为 IList 
获取
返回 _selectedAll
结束 获取
设置(value As IList)
_selectedAll = value
RaisePropertyChanged( SelectedAll
结束 设置
结束 物业





我尝试过:



SelectedIndex,SelectedItem,DataGrid而不是Lis tBox,

解决方案

解决了这篇文章:

在View Model中管理多个选择(.NET Metro Style Apps) [ ^ ]



谢谢!


Hi,
My Scenario:

Two ListBoxes - Listbox_01 and Listbox_02.
Listbox_01 ItemsSource is bind to a ObservableCollection in a ViewModel, SelectionMode set to Multiple, with attached property to limit the selection to 2 items. SelectedItems (dependency property) is bind to a property (IList) on ViewModel
Listbox_02 ItemsSource is bind to a property (IList) on a ViewModel (SelectedItems from Listbox_01). SelectionMode set to Single.
When I select items from Listbox_01 all should appear in a Listbox_02. This works for me. The question is, How can I link these two Listboxes to select and deselect items between them.
If I select two items from Listbox_01 they will be visible in Listbox_02. Then, when I select item from Listbox_02 it should get selected item (columns or content), fire command (RelayCommand), deselect that item on Listbox_01 and disappear from Listbox_02.

The problem here is that, when I click on Listbox_02 item there is always selection on Listbox_01 and the item that disappear from Listbox_02 is not correct (when I have two items, I click on the item_02 to close it, item_01 disappears)

Listbox_01 is only for selecting items. Listbox_02 is to execute command.

My code:
Listbox_01

<listv:ListViewSelectedItemBehavior SelectionMode="Multiple"

                                    ItemsSource="{Binding EmployeeViewM.MainActivity}"

                                    SelectedItemsList="{Binding EmployeeViewM.SelectedAll, Mode=OneWayToSource}"

                                    Background="{x:Null}"

                                    BorderBrush="{x:Null}"

                                    IsSynchronizedWithCurrentItem="False" >



Listbox_02

<listv:ListViewSelectedItemBehavior SelectionMode="Single"

                                    HorizontalAlignment="Left"

                                    ItemsSource="{Binding EmployeeViewM.SelectedAll}"

                                    SelectedItemsList="{Binding EmployeeViewM.SelectedAll, Mode=TwoWay}"

                                    Background="{x:Null}"

                                    BorderBrush="{x:Null}"

                                    IsSynchronizedWithCurrentItem="False">



SelectedAll IList

Public Property SelectedAll() As IList
    Get
        Return _selectedAll
    End Get
    Set(value As IList)
        _selectedAll = value
        RaisePropertyChanged("SelectedAll")
    End Set
End Property



What I have tried:

SelectedIndex, SelectedItem, DataGrid instead of ListBox,

解决方案

Solved thanks to this article:
Managing Multiple selection in View Model (.NET Metro Style Apps)[^]

Thank you!


这篇关于如何链接两个列表框以选择/取消选择和激活命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆