Hi,
When users enter edit mode for a cell in the xamDataGrid, the whole text of that cell is selected. How do I change it to put the caret position to where the mouse is so that users can insert the text where they want to without clicking again?
Thank you.
Hi Bach,
I've attached a sample that I believe should meet your requirement. The sample demonstrates how to set the caret position for the underlying editor in the cell. When a user clicks in the cell, it is placed into edit mode and the caret position is calculated with the TextBox.GetCharacterIndexFromPoint method.
Let me know if you have any questions on this.
Hi Rob,
Thank you for your reply. Your solution works great for XamTextEditor but when I tried with numeric column it seemed to break. the carotPos is always -1:
int carotPos = editorTextBox.GetCharacterIndexFromPoint(pos, false); if (carotPos == -1)
Would you know why?
Numeric columns use the XamNumericEditor which has a very different internal layout than the much simpler XamTextEditor. I rewrote the code so that it can now handle XamTextEditor and XamNumericEditor. Let me know if you have any questions on this.
Ahh if I change it to this then it will work:
private void CalculateCaretForNumericEditor(XamNumericEditor numEditor, Point pos) { TextBox editorTextBox = (TextBox)Utilities.GetDescendantFromType(numEditor, typeof(TextBox), true); bool wasCollapsed = false; if (editorTextBox.Visibility == System.Windows.Visibility.Collapsed) { wasCollapsed = true; editorTextBox.Visibility = System.Windows.Visibility.Visible; editorTextBox.Measure(numEditor.RenderSize); editorTextBox.Arrange(new Rect(0, 0, numEditor.RenderSize.Width, numEditor.RenderSize.Height)); }
int caretPos = editorTextBox.GetCharacterIndexFromPoint(pos, true);
if (wasCollapsed) editorTextBox.Visibility = System.Windows.Visibility.Collapsed;
if (caretPos >= 0) { editorTextBox.SelectionLength = 0; editorTextBox.SelectionStart = caretPos; } }
I'm using version 15.1 and this solution does not work for me. Specifically I'm getting a null return from Utilities.GetDescendantFromType.
Hi Dave,
You have to make sure to call StartEditMode() on the editor before you try to get the TextBox using GetDescendantFromType. If the editor is not in edit mode then there is no TextBox in its visual tree.