问题
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