Articles in this section

How to achieve the EditorSelectionBehaviour as SelectAll in GridTemplateColumn in WPF DataGrid (SfDataGrid)?

The EditorSelectionBehavior as SelectAll will not work when the GridTemplateColumn is in edit mode in WPF DataGrid (SfDataGrid). Because, the element loaded inside the edit template cannot be predicted. You can achieve this by programmatically selecting all the text whenever the edit element got focus.

In the following sample, SfTextBoxExt has been loaded as an edit element of GridTemplateColumn and all the text have been selected which is present inside the SfTextBoxExt by setting the SfTextBoxExt’s custom property AutoSelectable to true.

Refer to the following code snippet to achieve the EditorSelectionBehavior as SelectAll in GridTemplateColumn.

XAML

<syncfusion:SfDataGrid x:Name="grid"
                              AllowEditing="True"
                              AutoGenerateColumns="False"  
                              EditorSelectionBehavior="SelectAll"
                              ColumnSizer="Star"
                              ItemsSource="{Binding Source}
 
        <syncfusion:SfDataGrid.Columns>
        <syncfusion:GridTemplateColumn MappingName="EmployeeID" >
            <syncfusion:GridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding EmployeeID}"/>
                </DataTemplate>
            </syncfusion:GridTemplateColumn.CellTemplate>
            <syncfusion:GridTemplateColumn.EditTemplate>
                <DataTemplate>
                    <local:CustomTextBox x:Name="textBox" Text="{Binding EmployeeID}" AutoSelectable="True"/>
                </DataTemplate>
            </syncfusion:GridTemplateColumn.EditTemplate>
         </syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

 

CustomTextBox.cs

public class CustomTextBox : SfTextBoxExt
{
    public CustomTextBox() : base()
    {
 
    }
 
    public static bool GetAutoSelectable(DependencyObject obj)
    {
        return (bool)obj.GetValue(AutoSelectableProperty);
    }
 
    public static void SetAutoSelectable(DependencyObject obj, bool value)
    {
        obj.SetValue(AutoSelectableProperty, value);
    }
 
    public static readonly DependencyProperty AutoSelectableProperty =
        DependencyProperty.RegisterAttached("AutoSelectable",typeof(bool),typeof(CustomTextBox),new PropertyMetadata(false, AutoFocusableChangedHandler));
 
    private static void AutoFocusableChangedHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue != e.OldValue)
        {
            if ((bool)e.NewValue == true)
            {
                (d as SfTextBoxExt).GotFocus += OnGotFocusHandler;
            }
            else
            {
                (d as SfTextBoxExt).GotFocus -= OnGotFocusHandler;
            }
        }
    }
 
    private static void OnGotFocusHandler(object sender, RoutedEventArgs e)
    {
        (sender as SfTextBoxExt).SelectAll();
    }
 
}

View sample in GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied