F2 Should not Select All Text in RadGridView Cell
Environment
Product Version | 2019.1 415 |
Product | RadGridView for WPF |
Description
How to disable the text selection when a cell enters edit mode by pressing the F2 key.
Solution
- Create a custom RadGridView column and override its
PrepareCellForEdit
method. - In the method override, get the editing element (TextBox) and manually set its
SelectionStart
property.
public class CustomGridViewDataColumn : GridViewDataColumn
{
protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
{
var cell = base.PrepareCellForEdit(editingElement, editingEventArgs);
var tb = editingElement as TextBox;
if (tb != null && !(editingEventArgs is MouseButtonEventArgs))
{
tb.SelectionStart = tb.Text.Length;
}
return cell;
}
}
<telerik:RadGridView.Columns>
<local:CustomGridViewDataColumn DataMemberBinding="{Binding Name}" />
</telerik:RadGridView.Columns>