andrew4582

single click editing WPF

Oct 25th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1.  <!-- single click editing
  2.         ================================================== -->
  3.         <Style TargetType="{x:Type tk:GridViewCell}">
  4.             <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
  5.         </Style>
  6. //single click editing
  7.         private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  8.         {
  9.             DataGridCell cell = sender as DataGridCell;
  10.             if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
  11.             {
  12.                 if (!cell.IsFocused)
  13.                 {
  14.                     cell.Focus();
  15.                 }
  16.                
  17.                
  18.                 DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
  19.                 if (dataGrid != null)
  20.                 {
  21.                     if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
  22.                     {
  23.                         if (!cell.IsSelected)
  24.                             cell.IsSelected = true;
  25.                     }
  26.                     else
  27.                     {
  28.                         DataGridRow row = FindVisualParent<DataGridRow>(cell);
  29.                         if (row != null && !row.IsSelected)
  30.                         {
  31.                             row.IsSelected = true;
  32.                         }
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.  
  38.         static T FindVisualParent<T>(UIElement element) where T : UIElement
  39.         {
  40.             UIElement parent = element;
  41.             while (parent != null)
  42.             {
  43.                 T correctlyTyped = parent as T;
  44.                 if (correctlyTyped != null)
  45.                 {
  46.                     return correctlyTyped;
  47.                 }
  48.  
  49.                 parent = VisualTreeHelper.GetParent(parent) as UIElement;
  50.             }
  51.             return null;
  52.         }
Advertisement
Add Comment
Please, Sign In to add comment