8

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;
    }
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Erez
  • 6,405
  • 14
  • 70
  • 124
  • So you still want "c" to be selected in both lists, but when an item only exists in one list clear the selction from the other? – sa_ddam213 Jan 06 '13 at 09:44
  • sa_ddam213 - exactly! :) – Erez Jan 06 '13 at 09:47
  • Please keep in mind that your window already has a [Name](http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.name.aspx) property. So your newly created one is in conflict with the existing one. And your Name property is missing a change notification. – Clemens Jan 06 '13 at 10:34
  • Thanks Clemens, I'm aware of it, the code that I posted is just a snap code to ask the question, is not my real code and not my read view model. – Erez Jan 06 '13 at 10:48

1 Answers1

9

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

sa_ddam213
  • 42,848
  • 7
  • 101
  • 110