axelrotter
axelrotter

Reputation: 724

Getting rid of double Tabing in WPF Data Grid

The last to columns of my Data grid are read only and are supposed to ignore the tab completely since I set the Focusable property to False. The Data Grid is not custom it is only styled.

First tab

I can´t get the Tab to ignore the last two columns. I would like to jump from the 8th column right to the beginning of the next row. Instead, I have to tab through the last two columns before I get to the next row.

<Style x:Key="DataGridCellFocusVisualStyle">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle  StrokeThickness="2"  
                            Stroke="{StaticResource DarkGrayBrush}"   
                            SnapsToDevicePixels="true"
                            Margin="-5 0 0 0"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="BorderBrush" Value="{StaticResource DarkGrayBrush}" />
    <Setter Property="BorderThickness" Value="0 0 1 0" />
    <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <ContentControl Margin="5 0 0 0" Content="{TemplateBinding Content}" 
                                FocusVisualStyle="{StaticResource DataGridCellFocusVisualStyle}"/>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True" >
            <Setter Property="Foreground" Value="{StaticResource BlackBrush}"/>
            <Setter Property="Background" Value="{StaticResource WhiteBrush}"/>
        </Trigger>
        <Trigger Property="IsReadOnly" Value="True">
            <Setter Property="IsTabStop" Value="False"/>
        </Trigger>
    </Style.Triggers>
</Style>

This is the Column I want to jump over.

The Cell style is based on the CellStyle that we see in DataGridCellStyle in the XAML on top.

 <Style x:Key="CalculationNumericColumnCellStyle"
       BasedOn="{StaticResource LeschacoDataGridCellStyle}"
       TargetType="{x:Type DataGridCell}">
    <Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>

Upvotes: 1

Views: 343

Answers (2)

axelrotter
axelrotter

Reputation: 724

I answered my own question yet again. I saw that when I apply a Template to this part of the Data Grid, the Data Grid Cell seems to be seen by the TabManager as two controls in the visual tree. So the Focus Visual Style of the Data Grid Cell was the Dotted Rectangle and the Focus Visual Style of the Template was the Continuous Rectangle. So, lesson learned here is that you should not apply Templates to controls unless you absolutely have to. Heres the new XAML implementation of the Data Grid Cell if anyone is interested.

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="BorderBrush" Value="{StaticResource DarkGrayBrush}" />
    <Setter Property="BorderThickness" Value="0 0 1 0" />
    <Setter Property="FocusVisualStyle" Value="{StaticResource DataGridCellFocusVisualStyle}"/>
    <!--<Setter Property="VerticalAlignment" Value="Center"/>-->
    <!--<Setter Property="Padding" Value="-20 0 0 0"/>-->

    <Setter Property="Margin" Value="5 0 0 0"/>
    <Style.Triggers>
        <Trigger Property="DataGridCell.IsSelected" Value="True" >
            <Setter Property="Background" Value="{StaticResource LightBlueBrush}"/>
            <Setter Property="Foreground" Value="{StaticResource BlackBrush}"/>
        </Trigger>
        <Trigger Property="IsReadOnly" Value="True">
            <Setter Property="IsTabStop" Value="False"/>
        </Trigger>
    </Style.Triggers>
</Style>

Upvotes: 0

Naresh Ravlani
Naresh Ravlani

Reputation: 1620

Try the following style, it will skip all the columns where you have placed IsReadOnly = "True".

<Style TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <Trigger Property="IsReadOnly" Value="true">
            <Setter Property="IsTabStop" Value="False"/>
        </Trigger>
    </Style.Triggers>
</Style>

Upvotes: 2

Related Questions