Entering EditMode of right aligned DataGridTextColumn

Mar 26, 2009 at 9:42 AM
Hi

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.


Pommes


If someone has the same problem, here is the code that would fix the behavior (in most cases):

private

 

static bool PlaceCaretOnTextBox(TextBox textBox, Point position)

 

{

 

int characterCount = textBox.Text.Length;

 

 

int characterIndexSnap = textBox.GetCharacterIndexFromPoint(position, true);

 

 

int characterIndex = textBox.GetCharacterIndexFromPoint(position, false);

 

 

if (characterIndexSnap >= characterCount - 1 && characterIndexSnap != characterIndex)

 

{

 

textBox.CaretIndex = characterCount;

 

 

return true;

 

}

 

else

 

{

 

if (characterIndexSnap >= 0)

 

{

textBox.CaretIndex = characterIndexSnap;

 

return true;

 

}

}

 

return false;

 

}