This question is locked. New answers and comments are not allowed.
Just thought I'd share this snippet for others that may find this code useful. I had a read only grid that I wanted to execute a viewmodel command in response to a double click anywhere on a row (even on a row portion where a cell might not exist).
Here it is implemented via a Blend Behavior Trigger (but could be easily re factored for other uses):
XAML:
Here it is implemented via a Blend Behavior Trigger (but could be easily re factored for other uses):
| using System; |
| using System.Windows; |
| using System.Windows.Input; |
| using System.Windows.Interactivity; |
| using System.Linq; |
| using System.Collections.Generic; |
| using System.Windows.Media; |
| using Telerik.Windows.Controls; |
| using Telerik.Windows.Controls.GridView; |
| namespace MyApp.Behaviors.Triggers |
| { |
| public class RadGridViewRowMouseDoubleClickTrigger : TriggerBase<RadGridView> |
| { |
| private MouseButtonEventHandler _gridViewMouseLeftButtonDownHandler; |
| private DateTime _lastMouseDownDateTime = DateTime.Now; |
| private bool _hasFirstMouseDownOccured = false; |
| private Point _firstMouseDownPosition; |
| protected override void OnAttached() |
| { |
| if (AssociatedObject != null) |
| { |
| _gridViewMouseLeftButtonDownHandler = new MouseButtonEventHandler(OnGridViewMouseLeftButtonDown); |
| AssociatedObject.AddHandler(UIElement.MouseLeftButtonDownEvent, _gridViewMouseLeftButtonDownHandler, true); |
| } |
| } |
| protected override void OnDetaching() |
| { |
| if (AssociatedObject != null && _gridViewMouseLeftButtonDownHandler != null) |
| { |
| AssociatedObject.RemoveHandler(UIElement.MouseLeftButtonDownEvent, _gridViewMouseLeftButtonDownHandler); |
| } |
| } |
| private static bool IsGridViewRow(RadGridView radGridView, Point mousePosition, ref object rowDataContext) |
| { |
| GeneralTransform gt = radGridView.TransformToVisual(Application.Current.RootVisual); |
| Point mousePositionOffset = gt.Transform(mousePosition); |
| IEnumerable<UIElement> hitElements = VisualTreeHelper.FindElementsInHostCoordinates(mousePositionOffset, radGridView); |
| var gridViewRow = hitElements.OfType<GridViewRow>().FirstOrDefault(); |
| if (gridViewRow != null) |
| { |
| rowDataContext = gridViewRow.DataContext; |
| } |
| return gridViewRow != null; |
| } |
| private void OnGridViewMouseLeftButtonDown(object sender, MouseButtonEventArgs args) |
| { |
| DateTime currentMouseDownDateTime = DateTime.Now; |
| TimeSpan timeSpanBetweenButtonDowns = currentMouseDownDateTime - _lastMouseDownDateTime; |
| //First Mouse Down |
| if (timeSpanBetweenButtonDowns.TotalMilliseconds > 300 || _hasFirstMouseDownOccured == false) |
| { |
| _firstMouseDownPosition = args.GetPosition(AssociatedObject); |
| _hasFirstMouseDownOccured = true; |
| _lastMouseDownDateTime = DateTime.Now; |
| } |
| else |
| { |
| Point currentMousePosition = args.GetPosition(AssociatedObject); |
| object rowDataContext = null; |
| // Second Mouse Down within 300 miliseconds |
| // Mouse hasn't moved since previous left button down, |
| // and mouse pointer is within a GridViewRow |
| if ( (Math.Abs(_firstMouseDownPosition.X - currentMousePosition.X) < 4 && Math.Abs(_firstMouseDownPosition.Y - currentMousePosition.Y) < 4) |
| && IsGridViewRow(AssociatedObject, currentMousePosition, ref rowDataContext) |
| ) |
| { |
| InvokeActions(rowDataContext); |
| } |
| _hasFirstMouseDownOccured = false; |
| } |
| } |
| } |
| } |
XAML:
| <telerik:RadGridView |
| SelectionMode="Single" |
| IsReadOnly="True" |
| ItemsSource="{Binding Path=TestData}" |
| > |
| <i:Interaction.Triggers> |
| <Triggers:RadGridViewRowMouseDoubleClickTrigger> |
| <Triggers:RadGridViewRowMouseDoubleClickTrigger.Actions> |
| <Behaviors1:ExecuteCommandAction |
| Command="{Binding Path=TestCommand}" /> |
| </Triggers:RadGridViewRowMouseDoubleClickTrigger.Actions> |
| </Triggers:RadGridViewRowMouseDoubleClickTrigger> |
| </i:Interaction.Triggers> |
| </telerik:RadGridView> |
