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. Adding custom routed event of user control to xaml of a window in wpf

Adding custom routed event of user control to xaml of a window in wpf

Scheduled Pinned Locked Moved WPF
wpfcsharpcsswcfcom
2 Posts 1 Posters 0 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.
  • D Offline
    D Offline
    dashingsidds
    wrote on last edited by
    #1

    Hi Experts, I am little new to Command binding so this might be a trivial question to many. I know that we can add Command bindings in xaml of a window and give its correspondng property in viewmodel. This viewmodel will be given to the DataContext of the window. Something like the following --app.xaml.cs

    mainWindow.DataContext = viewModel;

    -- xaml

    lt;Button Grid.Row="1" HorizontalAlignment="Right" Margin="0,3,18,3" Name="button1" Width="110"
    Command="{Binding LoadCommand}">_Load</Button>

    -- viewmodel

    /// <summary>
    /// Gets the load command.
    /// </summary>
    /// <value>The load command.</value>
    public ICommand LoadCommand
    {
    get
    {
    if (m_LoadCommand == null)
    {
    m_LoadCommand = new RelayCommand(param => CanLoad(), param => Load());
    }

                return m\_LoadCommand;
            }
        }
    

    Here the relaycommand is a class which implements ICommand interface. CanLoad() and Load() are the methods which will get executed for canexecute and execute action of the relaycommand respectively. This is the click event of the button which is handled. I have a user control which has a custom routedevent registered in it and the user control is then used on a window. I am currently adding the event handler explicitly in code.

    //hook up event listeners on the actual UserControl instance
    this.ucCustomEvent1.CustomClick += new RoutedEventHandler(ucCustomEvent_CustomClick);
    //hook up event listeners on the main window (Window1)
    this.AddHandler(UserControlThatCreatesEvent.CustomClickEvent, new RoutedEventHandler(ucCustomEvent_CustomClick));

    I dont want to hook up the routedevent explicitly in code but in the xaml in the similar way as in the button example. I have uploaded the working sample code here for your perusal. Please help! Thanks in advance! Regards, Samar

    D 1 Reply Last reply
    0
    • D dashingsidds

      Hi Experts, I am little new to Command binding so this might be a trivial question to many. I know that we can add Command bindings in xaml of a window and give its correspondng property in viewmodel. This viewmodel will be given to the DataContext of the window. Something like the following --app.xaml.cs

      mainWindow.DataContext = viewModel;

      -- xaml

      lt;Button Grid.Row="1" HorizontalAlignment="Right" Margin="0,3,18,3" Name="button1" Width="110"
      Command="{Binding LoadCommand}">_Load</Button>

      -- viewmodel

      /// <summary>
      /// Gets the load command.
      /// </summary>
      /// <value>The load command.</value>
      public ICommand LoadCommand
      {
      get
      {
      if (m_LoadCommand == null)
      {
      m_LoadCommand = new RelayCommand(param => CanLoad(), param => Load());
      }

                  return m\_LoadCommand;
              }
          }
      

      Here the relaycommand is a class which implements ICommand interface. CanLoad() and Load() are the methods which will get executed for canexecute and execute action of the relaycommand respectively. This is the click event of the button which is handled. I have a user control which has a custom routedevent registered in it and the user control is then used on a window. I am currently adding the event handler explicitly in code.

      //hook up event listeners on the actual UserControl instance
      this.ucCustomEvent1.CustomClick += new RoutedEventHandler(ucCustomEvent_CustomClick);
      //hook up event listeners on the main window (Window1)
      this.AddHandler(UserControlThatCreatesEvent.CustomClickEvent, new RoutedEventHandler(ucCustomEvent_CustomClick));

      I dont want to hook up the routedevent explicitly in code but in the xaml in the similar way as in the button example. I have uploaded the working sample code here for your perusal. Please help! Thanks in advance! Regards, Samar

      D Offline
      D Offline
      dashingsidds
      wrote on last edited by
      #2

      Hi All, I am able to solve this problem of mine. The link given here explain it. Just to keep it brief i did it using something similar to the following lines of code.

      <c:MyUserControl local:CommandBehavior.RoutedEventName="MyCustomClick"
      local:CommandBehavior.TheCommandToRun="{Binding MyViewModelCommand}"/>

      CommandBehavior is a class given in the link above. Thanks everyone for giving in your valuable time. Regards, Samar

      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