I have the following:
<ListBox SelectedItem="{Binding SelectedItem}"
ItemsSource="{Binding items}" DisplayMemberPath="s"/>
<TextBlock Text="{Binding SelectedItem.s}"/>
This is definition of SelectedItem
public MemEntity SelectedItem {get; set;}
MemEntity
is a class containing
public String s {get; get;}.
Basically, I want s of the selected item to be shown in the TextBlock
(same property as shown in ListBox
). This doesn't work, so what am I doing wrong?
Try this,
then add a name to your ListBox as,
There are multiple way to do this. One option has already been provided in another answer that focusing on achieving the desired functionality by binding to a view element. Here is another option.
The view is unaware that selected item has changed. look into using
INotifyPropertyChanged
You can create a base ViewModel to encapsulate the repeated functionality
Have the view models inherit from this base class in order for the view to be aware of changes when binding.
The view will now be aware when ever the
SelectedItem
property changes and will update the view accordingly.