How can I bind a UserControl
's FrameworkElement
event to a view model command? I use MVVM and Prism so clear separation between the view the view-model would be nice.
I'd tried multiple things, and none of them worked:
<i:Interaction.Triggers>
<i:EventTrigger EventName="FrameworkElement.Unloaded">
<i:InvokeCommandAction Command="{Binding Unloaded}" />
</i:EventTrigger>
</i:Interaction.Triggers>
also using this tutorial http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html
local:FrameworkElementBehavior.UnloadedCommand="{Binding Unloaded}"
Do I have no choice but to add some functions to my code-behind?
Neither of the attempts above error out, but the command does not execute.
Here's my view model:
public class CustomerViewModel : PosViewModelBase
{
public ICommand Unloaded
{
get { return new UnloadedCommand(); }
}
public CustomerViewModel()
{
}
private class UnloadedCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
Debug.WriteLine("Customer stuff is out of view");
}
}
}