Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unselect all items when the SelectedItem is bound to something that doesn't exist in the list

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;
    }
}
like image 575
Erez Avatar asked Jan 06 '13 09:01

Erez


1 Answers

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 hereenter image description here

like image 101
sa_ddam213 Avatar answered Nov 06 '22 15:11

sa_ddam213