Select only one item in two ListViews

亡梦爱人 提交于 2021-02-19 06:02:58

问题


I have two ListView, each bound to a specific collection in my ViewModel and I want to be able to select only one item globally.

Each ListView has its SelectedItem property bound to the same property in the ViewModel.

My probleme is as follow: when I select an item in a ListView, and then select another item in the other ListView, the first item stays selected. I could achieve this with some code-behind, but I want to know if a pure XAML solution exists.

XAML:

<ListView SelectionMode="Single" ItemsSource="{Binding Path=MyList1}" SelectedItem="{Binding Path=MySelectedItem}">
   <ListView.ItemTemplate>
        <DataTemplate>
             <TextBlock Text="{Binding Name}" />
        </DataTemplate>
   </ListView.ItemTemplate>
</ListView>
<ListView SelectionMode="Single" ItemsSource="{Binding Path=MyList2}" SelectedItem="{Binding Path=MySelectedItem}">
   <ListView.ItemTemplate>
        <DataTemplate>
             <TextBlock Text="{Binding Title}" />
        </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

回答1:


I finally got it working, by just replacing SelectedItem by SelectedValue.

In this use case, both properties have the same behaviour, but the latter one handles correctly the unselection if the bound selected item is not in the list.




回答2:


You need to use Mode=TwoWay in your SelectedItem. It should work.



来源:https://stackoverflow.com/questions/28001534/select-only-one-item-in-two-listviews

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!