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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:56:57+00:00 2026-05-14T21:56:57+00:00

How can I bind a MouseDoubleClick event of a DataGrid to a delegate command

  • 0

How can I bind a MouseDoubleClick event of a DataGrid to a delegate command and get the selected item of that grid.? I want to do this in XAML file as I’m using Prism 2.0 and MVVM pattern.

  • 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-14T21:56:57+00:00Added an answer on May 14, 2026 at 9:56 pm

    Please look at attached properties, it will be helpful.
    The below is the code that will help, i had seen this somewhere but don’t remember the site or author’s name thanks to him anyways.

    /// <summary>
    /// The static class the defines the attached command DP for exposing MouseDoubleClick 
    /// event as  a command
    /// </summary>
    public static class CommandMouseDoubleClick
    {
        #region TheCommandToRun
    
        /// <summary>
        /// TheCommandToRun : The actual ICommand to run
        /// </summary>
        public static readonly DependencyProperty TheCommandToRunProperty =
            DependencyProperty.RegisterAttached("TheCommandToRun", typeof(ICommand),
            typeof(CommandMouseDoubleClick),
            new FrameworkPropertyMetadata((ICommand)null));
    
        /// <summary>  
        /// Gets the TheCommandToRun property.    
        /// </summary>  
        public static ICommand GetTheCommandToRun(DependencyObject d)
        {
            return (ICommand)d.GetValue(TheCommandToRunProperty);
        }
    
        /// <summary>  
        /// Sets the TheCommandToRun property.
        /// </summary>  
        public static void SetTheCommandToRun(DependencyObject d, ICommand value)
        {
            d.SetValue(TheCommandToRunProperty, value);
        }
    
        #endregion
    
        #region RoutedEventName
    
        /// <summary>  
        /// RoutedEventName : The event that should actually execute the  
        /// ICommand  
        /// </summary>  
        public static readonly DependencyProperty RoutedEventNameProperty =
            DependencyProperty.RegisterAttached("RoutedEventName", typeof(String),
            typeof(CommandMouseDoubleClick),
            new FrameworkPropertyMetadata((String)String.Empty,
                new PropertyChangedCallback(OnRoutedEventNameChanged)));
    
        /// <summary>  
        /// Gets the RoutedEventName property. 
        /// </summary>
        public static String GetRoutedEventName(DependencyObject d)
        {
            return (String)d.GetValue(RoutedEventNameProperty);
        }
    
        /// <summary>  
        /// Sets the RoutedEventName property.
        /// </summary>  
        public static void SetRoutedEventName(DependencyObject d, String value)
        {
            d.SetValue(RoutedEventNameProperty, value);
        }
    
        /// <summary> 
        /// Hooks up a Dynamically created EventHandler (by using the
        /// <see cref="MouseDoubleClickEventHooker">MouseDoubleClickEventHooker</see> class) that when  
        /// run will run the associated ICommand  
        /// </summary>
        private static void OnRoutedEventNameChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            String routedEvent = (String)e.NewValue;
            //If the RoutedEvent string is not null, create a new  
            //dynamically created EventHandler that when run will execute  
            //the actual bound ICommand instance (usually in the ViewModel)  
            if (!String.IsNullOrEmpty(routedEvent))
            {
                MouseDoubleClickEventHooker eventHooker = new MouseDoubleClickEventHooker();
                eventHooker.ObjectWithAttachedCommand = d;
                EventInfo eventInfo = d.GetType().GetEvent(routedEvent,
                    BindingFlags.Public | BindingFlags.Instance);
                //Hook up Dynamically created event handler  
                if (eventInfo != null)
                {
                    eventInfo.AddEventHandler(d,
                     eventHooker.GetNewEventHandlerToRunCommand(eventInfo));
                }
            }
        }
    
        #endregion
    }
    
    /// <summary> 
    /// Contains the event that is hooked into the source RoutedEvent 
    /// that was specified to run the ICommand 
    /// </summary> 
    sealed class MouseDoubleClickEventHooker
    {
        #region Properties
    
        /// <summary> 
        /// The DependencyObject, that holds a binding to the actual 
        /// ICommand to execute 
        /// </summary> 
        public DependencyObject ObjectWithAttachedCommand { get; set; }
    
        #endregion
    
        #region Public Methods
    
        /// <summary> 
        /// Creates a Dynamic EventHandler that will be run the ICommand 
        /// when the user specified RoutedEvent fires 
        /// </summary>
        /// <param name="eventInfo">The specified RoutedEvent EventInfo</param>
        ///<returns>An Delegate that points to a new EventHandler
        /// that will be run the ICommand</returns>
        public Delegate GetNewEventHandlerToRunCommand(EventInfo eventInfo)
        {
            Delegate del = null;
            if (eventInfo == null)
                throw new ArgumentNullException("eventInfo");
            if (eventInfo.EventHandlerType == null)
                throw new ArgumentException("EventHandlerType is null");
            if (del == null)
                del = Delegate.CreateDelegate(eventInfo.EventHandlerType, this,
                    GetType().GetMethod("OnEventRaised",
                    BindingFlags.NonPublic |
                    BindingFlags.Instance));
            return del;
        }
    
        #endregion
    
        #region Private Methods
        /// <summary> 
        /// Runs the ICommand when the requested RoutedEvent fires
        /// </summary>
        private void OnEventRaised(object sender, EventArgs e)
        {
            ICommand command = (ICommand)(sender as DependencyObject).
                GetValue(CommandMouseDoubleClick.TheCommandToRunProperty);
            if (command != null)
            {
                command.Execute(null);
            }
        }
        #endregion
    }
    

    The xaml could look something like below:

    <Label local:CommandMouseLeftButtonDown.RoutedEventName="MouseLeftButtonDown"  
           local:CommandMouseLeftButtonDown.TheCommandToRun="{Binding Path=MouseDownCommand, RelativeSource={RelativeSource TemplatedParent}}"
           local:CommandMouseDoubleClick.RoutedEventName="MouseDoubleClick"  
           local:CommandMouseDoubleClick.TheCommandToRun="{Binding Path=MouseDoubleClickCommand, RelativeSource={RelativeSource TemplatedParent}}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.