Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. <DataGrid DataGridCell.Selected="DataGrid_GotFocus" ItemsSource="{Binding Source={StaticResource itemView}}">
  2. <DataGrid.Columns>
  3. <DataGridTextColumn Header="Nom" Binding="{Binding Path=Name}"/>
  4. <DataGridTextColumn Header="Age" Binding="{Binding Path=Age}"/>
  5. </DataGrid.Columns>
  6. </DataGrid>
  7.  
  8. private void DataGrid_GotFocus(object sender, RoutedEventArgs e)
  9. {
  10. // Lookup for the source to be DataGridCell
  11. if (e.OriginalSource.GetType() == typeof(DataGridCell))
  12. {
  13. // Starts the Edit on the row;
  14. DataGrid grd = (DataGrid)sender;
  15. grd.BeginEdit(e);
  16. }
  17. }
  18.  
  19. <DataGrid DataGridCell.GotFocus="DataGrid_CellGotFocus" />
  20.  
  21. private void DataGrid_CellGotFocus(object sender, RoutedEventArgs e)
  22. {
  23. // Lookup for the source to be DataGridCell
  24. if (e.OriginalSource.GetType() == typeof(DataGridCell))
  25. {
  26. // Starts the Edit on the row;
  27. DataGrid grd = (DataGrid)sender;
  28. grd.BeginEdit(e);
  29.  
  30. Control control = GetFirstChildByType<Control>(e.OriginalSource as DataGridCell);
  31. if (control != null)
  32. {
  33. control.Focus();
  34. }
  35. }
  36. }
  37.  
  38. private T GetFirstChildByType<T>(DependencyObject prop) where T : DependencyObject
  39. {
  40. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(prop); i++)
  41. {
  42. DependencyObject child = VisualTreeHelper.GetChild((prop), i) as DependencyObject;
  43. if (child == null)
  44. continue;
  45.  
  46. T castedProp = child as T;
  47. if (castedProp != null)
  48. return castedProp;
  49.  
  50. castedProp = GetFirstChildByType<T>(child);
  51.  
  52. if (castedProp != null)
  53. return castedProp;
  54. }
  55. return null;
  56. }
  57.  
  58. <!-- SINGLE CLICK EDITING -->
  59. <Style TargetType="{x:Type dg:DataGridCell}">
  60. <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
  61. </Style>
  62.  
  63. //
  64. // SINGLE CLICK EDITING
  65. //
  66. private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  67. {
  68. DataGridCell cell = sender as DataGridCell;
  69. if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
  70. {
  71. if (!cell.IsFocused)
  72. {
  73. cell.Focus();
  74. }
  75. DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
  76. if (dataGrid != null)
  77. {
  78. if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
  79. {
  80. if (!cell.IsSelected)
  81. cell.IsSelected = true;
  82. }
  83. else
  84. {
  85. DataGridRow row = FindVisualParent<DataGridRow>(cell);
  86. if (row != null && !row.IsSelected)
  87. {
  88. row.IsSelected = true;
  89. }
  90. }
  91. }
  92. }
  93. }
  94.  
  95. static T FindVisualParent<T>(UIElement element) where T : UIElement
  96. {
  97. UIElement parent = element;
  98. while (parent != null)
  99. {
  100. T correctlyTyped = parent as T;
  101. if (correctlyTyped != null)
  102. {
  103. return correctlyTyped;
  104. }
  105.  
  106. parent = VisualTreeHelper.GetParent(parent) as UIElement;
  107. }
  108.  
  109. return null;
  110. }
  111.  
  112. <ResourceDictionary x:Class="YourNamespace.DataGridStyles"
  113. x:ClassModifier="public"
  114. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  115. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  116. <Style TargetType="DataGrid">
  117. <!-- Your DataGrid style definition goes here -->
  118.  
  119. <!-- Cell style -->
  120. <Setter Property="CellStyle">
  121. <Setter.Value>
  122. <Style TargetType="DataGridCell">
  123. <!-- Your DataGrid Cell style definition goes here -->
  124. <!-- Single Click Editing -->
  125. <EventSetter Event="PreviewMouseLeftButtonDown"
  126. Handler="DataGridCell_PreviewMouseLeftButtonDown" />
  127. </Style>
  128. </Setter.Value>
  129. </Setter>
  130. </Style>
  131. </ResourceDictionary>
  132.  
  133. using System.Windows.Controls;
  134. using System.Windows;
  135. using System.Windows.Input;
  136.  
  137. namespace YourNamespace
  138. {
  139. partial class DataGridStyles : ResourceDictionary
  140. {
  141.  
  142. public DataGridStyles()
  143. {
  144. InitializeComponent();
  145. }
  146.  
  147. // The code from the myermian's answer goes here.
  148. }
  149.  
  150. <DataGrid.Resources>
  151. <Style TargetType="{x:Type DataGridCell}" x:Key="SingleClickEditingCellStyle">
  152. <EventSetter Event="KeyDown" Handler="DataGridCell_KeyDown" />
  153. </Style>
  154. </DataGrid.Resources>
  155.  
  156. Private _endEditing As Boolean = False
  157.  
  158. Private Sub DataGrid_GotFocus(ByVal sender As Object, ByVal e As RoutedEventArgs)
  159. If Me._endEditing Then
  160. Me._endEditing = False
  161. Return
  162. End If
  163.  
  164. Dim cell = TryCast(e.OriginalSource, DataGridCell)
  165.  
  166. If cell Is Nothing Then
  167. Return
  168. End If
  169.  
  170. If cell.IsReadOnly Then
  171. Return
  172. End If
  173.  
  174. DirectCast(sender, DataGrid).BeginEdit(e)
  175. .
  176. .
  177. .
  178.  
  179. Private Sub DataGridCell_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
  180. If e.Key = Key.Enter Then
  181. Me._endEditing = True
  182. End If
  183. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement