I am trying to perform listbox changed event in WPF using MVVM. Please let me know how to do this selectionchanged event.
You can do it using
System.Windows.Interactivity
in your projectxmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Then
<ListBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding YourCommand}"
CommandParameter="{Binding YourCommandParameter}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
You would bind the SelectedItem
property of the listbox to your property on the ViewModel:
<ListBox SelectedItem="{Binding SelectedItem}" ...>
....
</ListBox>
In the property there always will be the selected item from the ListBox. If you really need to do something when the selection changes you can do it in the setter of that property:
public YourItem SelectedItem
{
get { return _selectedItem; }
set
{
if(value == _selectedItem)
return;
_selectedItem = value;
NotifyOfPropertyChange("SelectedItem");
// selection changed - do something special
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With