Josh
Josh

Reputation: 657

DataGrid Tabbing goes from cell to content

I have a datagrid consisting primarily of TemplateColumns. I am having the issue where when tabbing through the row, it appears to go to the cell, then the content within the cell (ie a textbox or togglebutton). The ideal situation is that tabbing through the columns puts the focus on the content and skips the cell. I may be interpreting what is happening incorrectly, but visually it appears to be the case. I have tried:

            <DataGridTemplateColumn Header="Group Value"
                                    MinWidth="30"
                                    Width=".02*">
                <DataGridTemplateColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="IsTabStop"
                                Value="False" />
                    </Style>
                </DataGridTemplateColumn.CellStyle>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding SomeBinding, 
                                                ValidatesOnDataErrors=True, 
                                                UpdateSourceTrigger=PropertyChanged}"
                                 IsReadOnly="{Binding RelativeSource={RelativeSource FindAncestor, 
                                               AncestorType={x:Type DataGrid}}, 
                                               Path=DataContext.IsReadOnly}"
                                 Style="{StaticResource TextBoxStyle}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

But that doesn't work as I assumed it would. Can anyone help me?

Thanks

Upvotes: 1

Views: 2657

Answers (2)

Mitesh
Mitesh

Reputation: 59

I tried many things which I found in different blog, but those did not work. But then I found one simple and effective technique which worked like champ!!!

This need only 2 steps.

1. Add Style in your resource file:

<!-- Cell Editing For DataGrid-->
<Style x:Key="CellEditingStyle" TargetType="DataGridCell">
    <Setter Property="IsTabStop" Value="False" />
</Style>

2. Add this style in your data Grid:

<DataGridTemplateColumn Header="{DynamicResource grdLoadsPerMonths}"
                        CellStyle="{StaticResource CellEditingStyle}">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=LoadsPerMonth, UpdateSourceTrigger=LostFocus}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

That is it. This has solved all the problem. No more work, no more class needs to be added.

Upvotes: 3

Khan
Khan

Reputation: 18142

You are correct, it does seem to focus the DataGridTemplateColumn before it focuses on the control within it while tabbing.

In my search for a solution, I came across this: http://iyalovoi.wordpress.com/2009/08/21/wpf-datagrid-tabbing-from-cell-to-cell-does-not-set-focus-on-control/

You can attach the FocusAttacher to the control within the template column, and it will steal the focus from it. It worked great for me.

Upvotes: 2

Related Questions