Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1028623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:25:07+00:00 2026-05-16T12:25:07+00:00

I am little new to Command binding so this might be a trivial question

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:25:08+00:00Added an answer on May 16, 2026 at 12:25 pm

    I’m not sure I fully understand your question but I hope one of my answers below helps you out.

    To attach a “direct” event handler in XAML, just do the following:

    <c:MyUserControl x:Name="uc1" CustomClick="uc1_CustomClickHandler"/>
    

    To hook up a handler for the (routed) event of one element (e.g. the CustomClick event in your example) to another element (e.g. the parent window):

    <Window c:MyUserControl.CustomClick="ucCustomEvent_CustomClick"/>
    

    Now, if you want to tie up an event in your UI to a Command in your ViewModel, you will need attached behaviors to do that. There are lots of frameworks around featuring different implementations of this. Here’s one you can try out: http://sachabarber.net/?p=514. It will allow you to do something like the following in your code:

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

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Little new to powershell. I am trying to locate a get-childitem like command that
I'm thinking about making a networked game. I'm a little new to this, and
Sorry, I'm new to SVN and I looked around a little for this. How
I'm a little new to the Java 5 annotations and I'm curious if either
I am pretty new to the Unity Application Block and am a little stuck
I'm fairly new to ASP.NET MVC, and I'm having a little trouble with scripts...
As a little side project, I am starting to build a new website for
A little help needed. I'm receiving an xml file similar to this: <?xml version=1.0
I'm thinking this should be easy, but I can't seem to figure this out.
I'm using the Ruby SVN bindings built with SWIG. Here's a little tutorial. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.