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 6711489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:08:45+00:00 2026-05-26T08:08:45+00:00

I am looking at various implementations of hooking a ICommand up to a control’s

  • 0

I am looking at various implementations of hooking a ICommand up to a control’s event. So for instance the GotFocus of a TextBox should call a GotFocusCommand in my View Model. I then got an idea to implement my own version (for my own learning) and it is working well, but I can only link one event to one command in the XAML.

( Basically I just use reflection to find the specified Event and then do a AddEventHandler that executes the command )

This works fine :

<Button 
  local:EventToCommand.Event="Click" 
  local:EventToCommand.Command="{Binding TestCommand}" 
  />  

But this does not :

<Button 
  local:EventToCommand.Event="Click" 
  local:EventToCommand.Command="{Binding ClickCommand}" 
  local:EventToCommand.Event="GotFocus" 
  local:EventToCommand.Command="{Binding GotFocusCommand}"
  />  

as you it leads to a duplicate attribute name error.

Would it be possible to do something like :

<Button>
  <Some Xaml Element>
    <local:EventToCommand Event="Click" Command="{Binding ClickCommand}" />
    <local:EventToCommand Event="GotFocus" Command="{Binding GotFocusCommand}" />
  </Some Xaml Element>
</Button>

to “map” multiple events to commands ?

  • 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-26T08:08:46+00:00Added an answer on May 26, 2026 at 8:08 am

    There are a couple of ways you could approach this, either using an Attached Property or inheriting from Button and adding your own DependencyProperty that contains a list of EventToCommand objects, and when you add to that collection you wire up the event to command. If this seems confusing, I can try to whip up some examples.

    C#

        public class EventedButton : Button
    {
        public static DependencyProperty EventCommandsProperty
            = DependencyProperty.Register("EventCommands", typeof(EventToCommandCollection), typeof(EventedButton), new PropertyMetadata(null));
    
    
        public EventToCommandCollection EventCommands
        {
            get
            {
                return this.GetValue(EventCommandsProperty) as EventToCommandCollection;
            }
            set
            {
                this.SetValue(EventCommandsProperty, value);
            }
        }
    
        public EventedButton()
        {
            this.EventCommands = new EventToCommandCollection(this);
        }
    }
    

    Xaml:

        <local:EventedButton>
            <local:EventedButton.EventCommands>
                <local:EventToCommand />
            </local:EventedButton.EventCommands>
        </local:EventedButton>
    

    Inside of EventToCommandCollection, you would attach/detach to the Event you wanted when items are added to the collection.

    UPDATE: Attached Property

    Here is some code to do the collection as an attached property:

    C#

            public static DependencyProperty CommandsProperty =
            DependencyProperty.RegisterAttached(
            "Commands",
            typeof(ICollection<EventToCommand>),
            typeof(DependencyObject),
            new PropertyMetadata(null, OnCommandsChanged));
    
    
        private static void OnCommandsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // Attach/Detach event handlers
        }
    
        public static void SetCommands(DependencyObject element, ICollection<EventToCommand> value)
        {
            element.SetValue(CommandsProperty, value);
        }
    
        public static ICollection<EventToCommand> GetCommands(DependencyObject element)
        {
            return (ICollection<EventToCommand>)element.GetValue(CommandsProperty);
        }
    

    Xaml:

        <local:EventedButton>
            <local:EventToCommand.Commands>
                <local:EventToCommandCollection>
                    <local:EventToCommand/>
                </local:EventToCommandCollection>
            </local:EventToCommand.Commands>
        </local:EventedButton>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for independent industry reports that compare and contrast the various source control
This link details on the various http client java implementations. I am looking for
I've been looking through various websites and came across multiple ways to make buttons.
I am looking into various OS designs in the hopes of writing a simple
Hey, I'm currently looking for various methods of implementing collision response in 2D video
All, I've spent a fair part of the day looking at various PCAP libraries
I was looking at the various windows styles flags, and I noticed that a
I'm looking into the various methods of rate limiting the Django admin login to
I've been looking at Boost and various other C++ libraries. The vast majority of
I'm looking for the smartest/various way of handling categories in App Engine. I see

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.