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?
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.
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With