Question

I have two lists with different ItemsSource but with SelectedItem bound to the same property - "Name".

First i'm choosing the item "c" in the right list so the item "c" in the left list is selected as well.

Than I selected another item in the right list but the "c" in the left list is still selected. I understand why it do that, but can I make it unselect the "c" in the right list ?

enter image description here

XAML:

 <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition  />
        <ColumnDefinition  />
    </Grid.ColumnDefinitions>

    <ListView SelectedItem="{Binding Name}" ItemsSource="{Binding lstNames1}"/>
    <ListView SelectedItem="{Binding Name}" ItemsSource="{Binding lstNames2}" Grid.Column="1"/>
</Grid>

Code behind:

 public partial class selected : Window
{
    public ObservableCollection<string> lstNames1 { get; set; }
    public ObservableCollection<string> lstNames2 { get; set; }

    public string Name { get; set; }


    public selected()
    {
        Names1 = new ObservableCollection<string> {"a1", "b1", "c"};
        Names2 = new ObservableCollection<string> { "a2", "b2", "c" };
        InitializeComponent();
        DataContext = this;
    }
}
Was it helpful?

Solution

If you switch the SelectedItem binding to SelectedValue this will behave how you want, The SelectedItem is not clearing because its not set to null because the other list has set a value, SelectedValue acts a bit differntly as it has to find an item or it will clear the SelectedItem on the list.

<ListView SelectedValue="{Binding Name}" ItemsSource="{Binding lstNames1}" />
<ListView SelectedValue="{Binding Name}" ItemsSource="{Binding lstNames2}" Grid.Column="1"/>

Hope that make sense :)

enter image description here enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top