Ravi Reddy
Ravi Reddy

Reputation: 123

Cell Edit in Wpf

I am building a wpf application, I have a DataGrid where user can edit cells, when I click on a cell for edit, the total text is selected like in this image:

current

I want the text to be like in this image when the user click on a cell for edit:

wannabe

Upvotes: 0

Views: 124

Answers (1)

Armen Mkrtchyan
Armen Mkrtchyan

Reputation: 921

CS

Implement CustomDataGrid, which inhherites DataGrid

public class CustomDataGrid : DataGrid
{
    protected override void OnPreparingCellForEdit(DataGridPreparingCellForEditEventArgs e)
    {
       base.OnPreparingCellForEdit(e);
       var textbox = e.EditingElement as TextBox;
       if (textbox == null) return;
       textbox.Focus();
       textbox.CaretIndex = textbox.Text.Length;
    }
}

XAML

<CustomDataGrid>
</CustomDataGrid>

Upvotes: 1

Related Questions