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?
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.
I'm using version 15.1 and this solution does not work for me. Specifically I'm getting a null return from Utilities.GetDescendantFromType.
I'm glad you were able to resolve your issue. Let me know if you have any further questions on this matter.
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; } }
In your sample project, just add the following to the resource and you will see the same issue as I am:
<Style TargetType="{x:Type igEditors:XamNumericEditor}"> <Setter Property="PromptChar" Value=""></Setter> <Setter Property="Mask" Value="{}{double:-15.7}"></Setter> </Style>