Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- single click editing
- ================================================== -->
- <Style TargetType="{x:Type tk:GridViewCell}">
- <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
- </Style>
- //single click editing
- private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- DataGridCell cell = sender as DataGridCell;
- if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
- {
- if (!cell.IsFocused)
- {
- cell.Focus();
- }
- DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
- if (dataGrid != null)
- {
- if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
- {
- if (!cell.IsSelected)
- cell.IsSelected = true;
- }
- else
- {
- DataGridRow row = FindVisualParent<DataGridRow>(cell);
- if (row != null && !row.IsSelected)
- {
- row.IsSelected = true;
- }
- }
- }
- }
- }
- static T FindVisualParent<T>(UIElement element) where T : UIElement
- {
- UIElement parent = element;
- while (parent != null)
- {
- T correctlyTyped = parent as T;
- if (correctlyTyped != null)
- {
- return correctlyTyped;
- }
- parent = VisualTreeHelper.GetParent(parent) as UIElement;
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment