Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight 5 double-click always returns ClickCount as 1

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)
        {
            if (e.ClickCount < 2) return;

            if (this.DoubleClickCommand != null) this.DoubleClickCommand.Execute(this.CommandParameter);
        }
    }

Everything seems to be fine except that it doesn't register multiple clicks. In OnMouseLeftButtonUp ClickCount always 1. Does anybody know why?

like image 306
katit Avatar asked Mar 19 '12 03:03

katit


2 Answers

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.

like image 191
herzmeister Avatar answered Nov 15 '22 13:11

herzmeister


Well, the bigger trouble here is that when you click on the rows of a DataGrid, MouseLeftButtonDown is not raised, because this click is being handled at the row level.

I've long given up on dealing with some controls directly. I've my own derived version of the DataGrid, DataForm and etc. This only makes my solution easy to roll out, because I don't use the vanilla versions anyway.

I added a new event called ClickIncludingHandled, it's a bit wordy but it properly describes what's going on and will nicely appear right below Click with IntelliSense - if the control has a Click event to begin with.

Anyway, below is my implementation of it. You can then subscribe to this event and use ClickCount to determine the number of clicks you want to capture. I noticed it is a bit slow, but it works cleanly.

public partial class DataGridBase : DataGrid
{
        public event MouseButtonEventHandler ClickIncludingHandled;

        public DataGridBase() : base()
        {
            this.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnClickInclHandled), true);
        }

        private void OnClickInclHandled(object sender, MouseButtonEventArgs e)
        {
            if (ClickIncludingHandled != null)
            {
                ClickIncludingHandled(sender, e);
            }
        }
}
like image 43
Doguhan Uluca Avatar answered Nov 15 '22 15:11

Doguhan Uluca