Mathias Colpaert
Mathias Colpaert

Reputation: 680

wpf datagridcell go in editmodus when focussed

I have a simple DataGrid that has 1 editable column and 1 readonly column.

When I press TAB or ENTER in editmodus, the next cell is focused. But the next cell does not automatically go into editmodus.

<DataGrid Name="DataGridMain" AutoGenerateColumns="False" SelectionUnit="Cell" SelectionMode="Single">
    <DataGrid.Columns>

        <DataGridTemplateColumn Header="Code">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Code}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="IsTabStop" Value="False"/>
                </Style>
            </DataGridTemplateColumn.CellStyle>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="Description">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Description}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Description}" Background="LightGray"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>

    </DataGrid.Columns>
</DataGrid>

What is the simplest way to go into editmodus upon focusing a cell?

Upvotes: 2

Views: 313

Answers (1)

Mathias Colpaert
Mathias Colpaert

Reputation: 680

I ended up simply subscribing to SelectedCellsChanged event of the datagrid, and calling BeginEdit().

<DataGrid Name="DataGridMain" SelectedCellsChanged="GridMainElements_SelectedCellsChanged" AutoGenerateColumns="False" SelectionUnit="Cell" SelectionMode="Single" >
    ...
</DataGrid>

And the event:

private void GridMainElements_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        Debug.WriteLine("Selected cells changed");

        if(e != null && e.AddedCells != null && e.AddedCells.Count == 1)
        {
            DataGridCellInfo selectedCell = e.AddedCells[0];

            if(selectedCell.Column == ColumnFormula || selectedCell.Column == ColumnNote)
            {
                GridMainElements.BeginEdit();
            }
        }
    }

Upvotes: 1

Related Questions