Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <DataGrid DataGridCell.Selected="DataGrid_GotFocus" ItemsSource="{Binding Source={StaticResource itemView}}">
- <DataGrid.Columns>
- <DataGridTextColumn Header="Nom" Binding="{Binding Path=Name}"/>
- <DataGridTextColumn Header="Age" Binding="{Binding Path=Age}"/>
- </DataGrid.Columns>
- </DataGrid>
- private void DataGrid_GotFocus(object sender, RoutedEventArgs e)
- {
- // Lookup for the source to be DataGridCell
- if (e.OriginalSource.GetType() == typeof(DataGridCell))
- {
- // Starts the Edit on the row;
- DataGrid grd = (DataGrid)sender;
- grd.BeginEdit(e);
- }
- }
- <DataGrid DataGridCell.GotFocus="DataGrid_CellGotFocus" />
- private void DataGrid_CellGotFocus(object sender, RoutedEventArgs e)
- {
- // Lookup for the source to be DataGridCell
- if (e.OriginalSource.GetType() == typeof(DataGridCell))
- {
- // Starts the Edit on the row;
- DataGrid grd = (DataGrid)sender;
- grd.BeginEdit(e);
- Control control = GetFirstChildByType<Control>(e.OriginalSource as DataGridCell);
- if (control != null)
- {
- control.Focus();
- }
- }
- }
- private T GetFirstChildByType<T>(DependencyObject prop) where T : DependencyObject
- {
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(prop); i++)
- {
- DependencyObject child = VisualTreeHelper.GetChild((prop), i) as DependencyObject;
- if (child == null)
- continue;
- T castedProp = child as T;
- if (castedProp != null)
- return castedProp;
- castedProp = GetFirstChildByType<T>(child);
- if (castedProp != null)
- return castedProp;
- }
- return null;
- }
- <!-- SINGLE CLICK EDITING -->
- <Style TargetType="{x:Type dg:DataGridCell}">
- <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;
- }
- <ResourceDictionary x:Class="YourNamespace.DataGridStyles"
- x:ClassModifier="public"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
- <Style TargetType="DataGrid">
- <!-- Your DataGrid style definition goes here -->
- <!-- Cell style -->
- <Setter Property="CellStyle">
- <Setter.Value>
- <Style TargetType="DataGridCell">
- <!-- Your DataGrid Cell style definition goes here -->
- <!-- Single Click Editing -->
- <EventSetter Event="PreviewMouseLeftButtonDown"
- Handler="DataGridCell_PreviewMouseLeftButtonDown" />
- </Style>
- </Setter.Value>
- </Setter>
- </Style>
- </ResourceDictionary>
- using System.Windows.Controls;
- using System.Windows;
- using System.Windows.Input;
- namespace YourNamespace
- {
- partial class DataGridStyles : ResourceDictionary
- {
- public DataGridStyles()
- {
- InitializeComponent();
- }
- // The code from the myermian's answer goes here.
- }
- <DataGrid.Resources>
- <Style TargetType="{x:Type DataGridCell}" x:Key="SingleClickEditingCellStyle">
- <EventSetter Event="KeyDown" Handler="DataGridCell_KeyDown" />
- </Style>
- </DataGrid.Resources>
- Private _endEditing As Boolean = False
- Private Sub DataGrid_GotFocus(ByVal sender As Object, ByVal e As RoutedEventArgs)
- If Me._endEditing Then
- Me._endEditing = False
- Return
- End If
- Dim cell = TryCast(e.OriginalSource, DataGridCell)
- If cell Is Nothing Then
- Return
- End If
- If cell.IsReadOnly Then
- Return
- End If
- DirectCast(sender, DataGrid).BeginEdit(e)
- .
- .
- .
- Private Sub DataGridCell_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
- If e.Key = Key.Enter Then
- Me._endEditing = True
- End If
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement