WPF list binding: SelectedItem not set

I have a ListBox simplified for the next XAML

<ListBox ItemsSource="{Binding Properties}" 
     DisplayMemberPath="Name" 
     SelectedItem="SelectedProperty" />

and in my ViewModel:

private List<Property> propertyList;
private Property selectedProperty;
public List<Property> Properties 
{ 
    get 
    { 
        return propertyList; 
    } 
    set 
    { 
        propertyList = value;
        NotifyPropertyChanged("Properties");
    } 
}
public Property SelectedProperty
{
    get
    {
        return selectedProperty;
    }
    set
    {
        NotifyPropertyChanged("SelectedProperty");
        selectedProperty= value;
    }
}

The Favorites field fills perfectly, but no matter what I try, I seem to be unable to get SelectedProperty to update when I select an item in my list. I tried to switch everything around to use ObservableCollectioninstead Listand add an event handler for CollectionChanged, but that didn't work.

I am sure that I am missing something stupid, and I do not see a tree for trees. I get to the end of my cable and need someone to intervene and help.

+3
source share
1 answer

You need to bind to SelectedProperty:

<ListBox ItemsSource="{Binding Properties}" 
 DisplayMemberPath="Name" 
 SelectedItem="{Binding SelectedProperty}"  />
+9
source

All Articles