This is a migrated thread and some comments may be shown as answers.

Caret-position not as expected when starting to edit

3 Answers 474 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Magnus
Top achievements
Rank 1
Magnus asked on 29 Aug 2017, 11:04 AM

I've got a GridView which has AutoGenerateColumn set to true, and I don't apply any custom styles or templates, so it's pretty basic.

I've noticed that when I want to edit a cell and clicks with the mouse the caret always end up one step to the left.

 

I've tried different EditTriggers and SelectionUnits, but it doesn't change the behaviour.

 

Not a major issue, but still a bit annoying.

3 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 01 Sep 2017, 08:45 AM
Hello Magnus,

To alter this behavior you can subscribe to the PreparedCellForEdit event of RadGridView and manually update the CaretIndex of the TextBox that presents the cell editor. Here is an example in code:
private void RadGridView_PreparedCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
{
    if (e.EditingEventArgs is MouseButtonEventArgs)
    {
        var textBox = (TextBox)e.EditingElement;
        textBox.CaretIndex++;
    }
}

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Magnus
Top achievements
Rank 1
answered on 04 Sep 2017, 08:09 AM

Thanks for your reply. I tried your suggstion earlier, but found that it didn't always work, since the caret index sometimes already were correct. This is my solution, using the actual clicked TextBlock to extract the caret index:

 

public MyRadGridView()
{
    this.PreparedCellForEdit += OnPreparedCellForEdit;
}
 
private int lastClickOffset;
 
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
    var textBlock = e.OriginalSource as TextBlock;
    if (textBlock != null)
    {
        textBlock.CaptureMouse();
 
        var relMousePosition = e.GetPosition(textBlock);
        var textPointer = textBlock.GetPositionFromPoint(relMousePosition, true);
 
        lastClickOffset = -textPointer.GetOffsetToPosition(textBlock.ContentStart) - 1;
    }
 
    base.OnPreviewMouseLeftButtonDown(e);
}
 
private void OnPreparedCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
{
    var mouseEvents = e.EditingEventArgs as MouseButtonEventArgs;
    if (mouseEvents != null)
    {
        var textBox = (TextBox)e.EditingElement;
        textBox.CaretIndex = lastClickOffset;
    }
}
0
Accepted
Martin Ivanov
Telerik team
answered on 06 Sep 2017, 07:17 AM
Hello Magnus,

I am glad to hear that you managed to achieve your requirement. And thank you for sharing your solution here.

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Magnus
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Magnus
Top achievements
Rank 1
Share this question
or