Alex Rivera | Logout

Silverlight 5 double-click always returns ClickCount as 1

Asked 2012-03-19T03:54:44.170
8

I created behavior for DataGrid to detect double-click:

public class DataGridDoubleClickBehavior : Behavior<DataGrid>    
    {
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
            "CommandParameter",
            typeof(object),
            typeof(DataGridDoubleClickBehavior),
            new PropertyMetadata(null));        

        public object CommandParameter
        {
            get { return GetValue(CommandParameterProperty); }            
            set { SetValue(CommandParameterProperty, value); }
        }

        public static readonly DependencyProperty DoubleClickCommandProperty = DependencyProperty.Register(
            "DoubleClickCommand",
            typeof(ICommand),
            typeof(DataGridDoubleClickBehavior),
            new PropertyMetadata(null));       

        public ICommand DoubleClickCommand
        {
            get { return (ICommand)GetValue(DoubleClickCommandProperty); }            
            set { SetValue(DoubleClickCommandProperty, value); }
        }

        protected override void OnAttached()
        {
            this.AssociatedObject.LoadingRow += this.OnLoadingRow;
            this.AssociatedObject.UnloadingRow += this.OnUnloadingRow;

            base.OnAttached();
        }

        protected override void OnDetaching()
        {
            this.AssociatedObject.LoadingRow -= this.OnLoadingRow;
            this.AssociatedObject.UnloadingRow -= this.OnUnloadingRow;

            base.OnDetaching();
        } 

        private void OnLoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.MouseLeftButtonUp += this.OnMouseLeftButtonUp;
        }

        private void OnUnloadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.MouseLeftButtonUp -= this.OnMouseLeftButtonUp;
        }

        private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            i
Edit
Report

2 Answers

11

I found a very simple solution. Just replace the event handler registration syntax

myDataGrid.MouseLeftButtonDown += this.MyDataGrid_MouseLeftButtonDown;

with the AddHandler syntax

myDataGrid.AddHandler(DataGrid.MouseLeftButtonDownEvent,
    new MouseButtonEventHandler(this.MyDataGrid_MouseLeftButtonDown),
    handledEventsToo: true)

That way the magic boolean handledEventsToo argument can be specified.

This will, well, handle handled events too.

answered 2012-04-19T13:58:27.713
0

I ran into this exact same problem. I couldn't manage to resolve it in any sensible way, so worked around it like this:

    private DateTime _lastClickTime;
    private WeakReference _lastSender;

    private void Row_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        var now = DateTime.Now;
        if ((now - _lastClickTime).TotalMilliseconds < 200 && _lastSender != null && _lastSender.IsAlive && _lastSender.Target == sender)
        {
            if (Command != null)
            {
                Command.Execute(CommandParameter);
            }
        }
        _lastClickTime = now;
        _lastSender = new WeakReference(sender);
    }

It's dirty but it works.

answered 2012-03-30T04:09:17.210

Your Answer