Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Routed Event Args Null

Routed Event Args Null

Scheduled Pinned Locked Moved WPF
csswpfwcf
2 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    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;
    }

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      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;
      }

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The problem isn't that the EventArgs object is null; it's that the EventArgs object isn't being passed as the command parameter to your RelayCommand. If you're using the Microsoft.Xaml.Behaviors.Wpf[^] package, you need to set PassEventArgsToCommand="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 the InvokeCommandAction 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups