I'm developing a MVVM app with WPF, C# and .NET Framework 4.6.1.
I'm trying to implement ListBox SelectionChanged
event using MVVM. To do it I have done this:
Install nuget package: PM> Install-Package System.Windows.Interactivity.WPF
.
Add xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
on xaml.
Add this code to my ListBox
in xaml:
<ListBox x:Name="listBoxTrzType" Grid.Column="1" Margin="10,0,25,0" VerticalAlignment="Center" Height="25" ItemsSource="{Binding TrzTypes}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="SelectionChanged">
<interactivity:InvokeCommandAction Command="{Binding ListBoxTrzTypeSelectionChanged}"
CommandParameter="{Binding ElementName=listBoxTrzType, Path=SelectedItem}">
</interactivity:InvokeCommandAction>
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
</ListBox>
On ViewModel I have:
public ICommand ListBoxTrzTypeSelectionChanged
{
get { return new DelegateCommand(TrzTypeSelectionChanged); }
}
private void TrzTypeSelectionChanged()
{
throw new NotImplementedException();
}
And DelegateCommand
class:
public class DelegateCommand : ICommand
{
private readonly Action _action;
public DelegateCommand(Action action)
{
_action = action;
}
public void Execute(object parameter)
{
_action();
}
public bool CanExecute(object parameter)
{
return true;
}
#pragma warning disable 67
public event EventHandler CanExecuteChanged { add { } remove { } }
#pragma warning restore 67
}
But my problem is that I don't know how to pass CommandParameter="{Binding ElementName=listBoxTrzType, Path=SelectedItem}"
to private void TrzTypeSelectionChanged()
.
I've been searching a lot on Internet and I haven't found any example (only examples with CanExecute
).
How do I have to modify my ViewModel
class to access SelectedItem
parameter?