✅ enable single-click cell editing in Avalonia DataGrid

Hi all, In my Avalonia UI DataGrid, clicking on any cell currently selects the entire row, and I have to click the cell one more time before I can start typing. I'd like to achieve: 1. After a single click on the cell, the user should be able to immediately start typing without any additional clicks. (It's okay to disable full-row selection because we don't need it.) Is there a built-in property or a recommended workaround to achieve single-click cell editing without row selection? Thanks in advance! The Column that should allow input:
<DataGridTextColumn Header="SplitLength"
Binding="{Binding SplitLength}"
IsReadOnly="False"
Width="1*"
Tag="SplitLength"/>
<DataGridTextColumn Header="SplitLength"
Binding="{Binding SplitLength}"
IsReadOnly="False"
Width="1*"
Tag="SplitLength"/>
The DataGrid:
<DataGrid Name="Table" ItemsSource="{Binding PartsFiltered}"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserSortColumns="True"
GridLinesVisibility="All"
FontSize="5"
BorderThickness="1" BorderBrush="Gray"
CellEditEnding="Table_CellEditEnding"
CellPointerPressed="Table_OnCellPointerPressed">
<DataGrid Name="Table" ItemsSource="{Binding PartsFiltered}"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserSortColumns="True"
GridLinesVisibility="All"
FontSize="5"
BorderThickness="1" BorderBrush="Gray"
CellEditEnding="Table_CellEditEnding"
CellPointerPressed="Table_OnCellPointerPressed">
current code behind for "CellPointerPressed":
private void Table_OnCellPointerPressed(object? sender, DataGridCellPointerPressedEventArgs e)
{
// only for the SplitLength column
if (e.Column is DataGridTextColumn textColumn &&
textColumn.Tag?.ToString() == "SplitLength")
{
// i guess i need to do something here?
}
}
private void Table_OnCellPointerPressed(object? sender, DataGridCellPointerPressedEventArgs e)
{
// only for the SplitLength column
if (e.Column is DataGridTextColumn textColumn &&
textColumn.Tag?.ToString() == "SplitLength")
{
// i guess i need to do something here?
}
}
3 Replies
Suchacoolguy
SuchacoolguyOP2d ago
hey, Thanks for the comment! It works perfectly now. :blushowo:
private void Table_OnCellPointerPressed(object? sender, DataGridCellPointerPressedEventArgs e)
{
if (e.Column is DataGridTextColumn col &&
col.Tag?.ToString() == "SplitLength")
{
var grid = this.FindControl<DataGrid>("Table");
if (grid == null) return;

grid.SelectedItem = e.Row.DataContext;

grid.CurrentColumn = e.Column;

e.Cell.Focus();

grid.BeginEdit();

e.PointerPressedEventArgs.Handled = true;
}
}
private void Table_OnCellPointerPressed(object? sender, DataGridCellPointerPressedEventArgs e)
{
if (e.Column is DataGridTextColumn col &&
col.Tag?.ToString() == "SplitLength")
{
var grid = this.FindControl<DataGrid>("Table");
if (grid == null) return;

grid.SelectedItem = e.Row.DataContext;

grid.CurrentColumn = e.Column;

e.Cell.Focus();

grid.BeginEdit();

e.PointerPressedEventArgs.Handled = true;
}
}
MODiX
MODiX2d ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?