The normal behavior is the following. When entering the EditMode of a cell of a DataGridTextColumn using the Mouse (2 clicks in a cell), normally (if clicked inside the text) the caret if placed near the position where the click happened.
If I change the TextAlignment for the Element and the EditingElement to right alignment this behavior does not work anymore. Most of the time the whole text of that cell is selected in this case.
This is no direct problem of the DataGrid, because it seems that the Framework method: TextBox.GetCharacterIndexFromPoint(...) (used in DataGridTextColumn.PlaceCaretOnTextBox(...)) does not work correctly in this case.
Am I missing something (some setting maybe) or is this really a problem?
There is a workaround to fix this problem but this means that I need to override DataGridTextColumn.PrepareCellForEdit(...) method just to replace the content of the DataGridTextColumn.PlaceCaretOnTextBox(...) method. Is there be a better way to do this? I
normally don't want to copy the code of the DataGridTextColumn.PrepareCellForEdit(...) method just to do that.
If someone has the same problem, here is the code that would fix the behavior (in most cases):
bool PlaceCaretOnTextBox(TextBox textBox,
int characterCount = textBox.Text.Length;
int characterIndexSnap = textBox.GetCharacterIndexFromPoint(position,
int characterIndex = textBox.GetCharacterIndexFromPoint(position,
if (characterIndexSnap >= characterCount - 1 && characterIndexSnap != characterIndex)
textBox.CaretIndex = characterCount;
if (characterIndexSnap >= 0)
textBox.CaretIndex = characterIndexSnap;