Routed Event Args Null
-
I have a user control with a custom Routed Event. I'm trying to pass event args:
public class NavigationViewFilterEventArgs : RoutedEventArgs
{
public List<LookupEntity> StatusFilters { get; private set; }public List<LookupEntity> FilterItems { get; private set; } public NavigationViewFilterEventArgs(RoutedEvent e, List<LookupEntity> statusFilters, List<LookupEntity> filterItems) : base(e) { StatusFilters = statusFilters; FilterItems = filterItems; }
}
Here's the UserControl code behind:
public static readonly RoutedEvent FilterExecutedEvent =
EventManager.RegisterRoutedEvent("FilterExecuted",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(NavigationView));public event RoutedEventHandler FilterExecuted
{
add { AddHandler(FilterExecutedEvent, value); }
remove { RemoveHandler(FilterExecutedEvent, value); }
}private void RaiseFilterExecutedEvent()
{
var args = new NavigationViewFilterEventArgs(FilterExecutedEvent, StatusFilters, FilterItems);
RaiseEvent(args);
}The control is on the Main Window:
<ctrls:NavigationView Grid.Row="7"
Caption="Companies"
StatusFilters="{Binding DataContext.CompanyStatusFilterItems,
ElementName=window, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"><i:Interaction.Triggers> <i:EventTrigger EventName="FilterExecuted"> <i:InvokeCommandAction Command="{Binding DataContext.CompanyFilterExecutedEventCommand, ElementName=window}"/> </i:EventTrigger> </i:Interaction.Triggers>
</ctrls:NavigationView>
And finally the handler in the Main Window view model:
private ICommand _CompanyFilterExecutedEventCommand;
public ICommand CompanyFilterExecutedEventCommand
{
get
{
if (_CompanyFilterExecutedEventCommand == null)
_CompanyFilterExecutedEventCommand = new RelayCommand<NavigationViewFilterEventArgs>(p => CompanyFilterExecutedEventExecuted(p), p => CompanyFilterExecutedEventCanExecute());
return _CompanyFilterExecutedEventCommand;
}
}private bool CompanyFilterExecutedEventCanExecute()
{
return true;
} -
I have a user control with a custom Routed Event. I'm trying to pass event args:
public class NavigationViewFilterEventArgs : RoutedEventArgs
{
public List<LookupEntity> StatusFilters { get; private set; }public List<LookupEntity> FilterItems { get; private set; } public NavigationViewFilterEventArgs(RoutedEvent e, List<LookupEntity> statusFilters, List<LookupEntity> filterItems) : base(e) { StatusFilters = statusFilters; FilterItems = filterItems; }
}
Here's the UserControl code behind:
public static readonly RoutedEvent FilterExecutedEvent =
EventManager.RegisterRoutedEvent("FilterExecuted",
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(NavigationView));public event RoutedEventHandler FilterExecuted
{
add { AddHandler(FilterExecutedEvent, value); }
remove { RemoveHandler(FilterExecutedEvent, value); }
}private void RaiseFilterExecutedEvent()
{
var args = new NavigationViewFilterEventArgs(FilterExecutedEvent, StatusFilters, FilterItems);
RaiseEvent(args);
}The control is on the Main Window:
<ctrls:NavigationView Grid.Row="7"
Caption="Companies"
StatusFilters="{Binding DataContext.CompanyStatusFilterItems,
ElementName=window, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"><i:Interaction.Triggers> <i:EventTrigger EventName="FilterExecuted"> <i:InvokeCommandAction Command="{Binding DataContext.CompanyFilterExecutedEventCommand, ElementName=window}"/> </i:EventTrigger> </i:Interaction.Triggers>
</ctrls:NavigationView>
And finally the handler in the Main Window view model:
private ICommand _CompanyFilterExecutedEventCommand;
public ICommand CompanyFilterExecutedEventCommand
{
get
{
if (_CompanyFilterExecutedEventCommand == null)
_CompanyFilterExecutedEventCommand = new RelayCommand<NavigationViewFilterEventArgs>(p => CompanyFilterExecutedEventExecuted(p), p => CompanyFilterExecutedEventCanExecute());
return _CompanyFilterExecutedEventCommand;
}
}private bool CompanyFilterExecutedEventCanExecute()
{
return true;
}The problem isn't that the
EventArgs
object isnull
; it's that theEventArgs
object isn't being passed as the command parameter to yourRelayCommand
. If you're using the Microsoft.Xaml.Behaviors.Wpf[^] package, you need to setPassEventArgsToCommand="True"
on your<i:InvokeCommandAction>
element. If you're using Prism, that will pass the event args automatically if the command parameter is not set. But that would use a different namepsace prefix on theInvokeCommandAction
element. 6: Advanced MVVM Scenarios Using the Prism Library 5.0 for WPF | Microsoft Docs[^] Other platforms will have different approaches. There are quite a few documented on this SO thread: wpf - MVVM Passing EventArgs As Command Parameter - Stack Overflow[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer