c# – Missing SelectedItem in ListBox

Question:

There is a form. Here are its contents

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <ListBox ItemsSource="{Binding Col1}" DisplayMemberPath="Name" SelectedValuePath="Sum" SelectedItem="{Binding Selected, Mode=TwoWay}"/>
    <ListBox Grid.Column="1" ItemsSource="{Binding Col2}" DisplayMemberPath="Name" SelectedValuePath="Sum" SelectedItem="{Binding Selected, Mode=TwoWay}" MouseDoubleClick="ListBox_MouseDoubleClick"/>
</Grid>

Here is her background (it's a view)

public partial class MainWindow : Window, INotifyPropertyChanged
{
    Output _selected;

    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<Output> Col1 { get; set; }
    public ObservableCollection<Output> Col2 { get; set; }
    public Output Selected
    {
        get
        {
            return _selected;
        }
        set
        {
            _selected = value;
            OnPropertyChanged(nameof(Selected));
        }
    }
    public MainWindow()
    {
        Col1 = new ObservableCollection<Output>
        {
            new Output {Name="name_1", Sum=1 },
            new Output {Name="name_2", Sum=2 },
            new Output {Name="name_3", Sum=3 },
            new Output {Name="name_4", Sum=4 }
        };

        Col2 = new ObservableCollection<Output>
        {
            Col1[2],
            Col1[3],
            new Output {Name="name_5", Sum=5 },
            new Output {Name="name_6", Sum=6 }
        };
        DataContext = this;
        InitializeComponent();
    }

    private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        Selected = null;
    }

    void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

I launch it, select the first item in the left sheet. All right. I select the last item in the left sheet (name 4). The corresponding item is also highlighted in the second sheet. Now I select the last item in the second sheet …

It was expected that this would unselect the selection in the left sheet, because this item is not in the bound collection. But the selection remains. Why?

UPD:

But the saddest thing turned out to be not even this, but the fact that when you move tabs through the sheets (or with the mouse, clicking on the items already selected visually), the Selected property does not change. This will be visible if you bind the title of the window to it.

Title="{Binding Selected.Name}"

Answer:

It seems like in case of problems with binding, it is simply ignored, i.e. in this case, there will be no change in the selection in the listbox if you try to specify an element that does not exist in the ItemsSource. If the behavior of listboxes given by the author of the question is really necessary, then you need to do 2 Selected fields, i.e. each listbox has its own, and a bit of logic in the set blocks. for example, reinstalling or zeroing the adjacent Select if necessary (well, and defeat the looping of setting these fields from each other)

As for the "most sad" thing – I think this is just a consequence of broken binding.

Scroll to Top