WPF DataGrid Won’t Enter Edit Mode From TAB Key, Only From Mouse Click

I have a DataGrid which is refusing to enter edit mode at all when I use the TAB key to tab through the cells. The focus lands on each cell itself, but the edit controls never appear. Clicking on the cells makes the edit controls appear as expected.

I’ve found several resources on the problem of having to press the TAB button twice to focus the edit control such as this one – I wish I had that problem because I can see how to solve it – as it stands my DataGrid doesn’t seem to be providing standard out-of-the-box behaviour and I have no idea why. To eliminate any possible interference from styles or behaviours in my solution, I created a blank WPF project (VS 2012, .NET 4.5.1) and added a single DataGrid to the main window with the simplest possible view model, shown below, which still exhibits the same problem.

<DataGrid ItemsSource="{Binding Foos}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Colour" IsReadOnly="True" Binding="{Binding Colour}" />
        <DataGridTemplateColumn Header="Larger than Elephant">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding IsLargerThanAnElephant}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Shape">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Shape}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox Text="{Binding Shape}" IsEditable="True">
                        <ComboBox.Items>
                            <ComboBoxItem>Red</ComboBoxItem>
                            <ComboBoxItem>Green</ComboBoxItem>
                            <ComboBoxItem>Blue</ComboBoxItem>
                        </ComboBox.Items>
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Size">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Size}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Size}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>


Source: .net

Leave a Reply