Skip to content

Repository files navigation

How to set the cursor position based on mouse clicked position while editing in WPF DataGrid (SfDataGid)?

About the sample

This example illustrates how to set the cursor position based on mouse clicked position while editing in WPF DataGrid (SfDataGid)?

WPF DataGrid (SfDataGrid) cell enters into edit mode, cursor is placed based on EditorSelectionBehavior property. You can set the cursor position based on mouse clicked position while editing by overriding the OnEditElementLoaded method in GridCellTextBoxRenderer.

//customize the TextBoxCellRenderer 
this.sfDataGrid.CellRenderers.Remove("TextBox");
this.sfDataGrid.CellRenderers.Add("TextBox", new GridCellTextBoxRendererExt());

public class GridCellTextBoxRendererExt : GridCellTextBoxRenderer
{  
     protected override void OnEditElementLoaded(object sender, RoutedEventArgs e)
     {
          base.OnEditElementLoaded(sender, e);
          var uiElement = sender as TextBox;
          //set the CaretIndex based on clicked text position
          uiElement.CaretIndex = uiElement.GetCharacterIndexFromPoint(Mouse.GetPosition(uiElement), true);
     }
}

WPF DataGrid (SfDataGrid) provides support for various built-in column types. Each column has its own properties and renderer for more details please refer the below documentation link.

Documentation Link: https://help.syncfusion.com/wpf/datagrid/column-types

Shows the cursor position based on mouse clicked position while editing in SfDataGrid

Take a moment to peruse the WPF DataGrid - Editing documentation, where you can find about editing with code examples.

KB article - How to set the cursor position based on mouse clicked position while editing in WPF DataGrid (SfDataGid)?

Requirements to run the demo

Visual Studio 2015 and above versions

About

This demo shows how to set the cursor position based on mouse clicked position while editing in WPF SfDataGrid.

Topics

Resources

Stars

0 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages