Kishore Kumar
Kishore Kumar

Reputation: 12874

Remove Focus from Particular Columns in WPF DataGrid

I have a WPF DataGrid.

When a user press the tab key i dont want to get focus on 1st column and also 3rd column so when i reach a row i should be in the second column and when i press tab key again and reach 3 column it sholud move to 4th column.

How can i do this in WPF DataGrid

Upvotes: 0

Views: 2361

Answers (2)

Dan Busha
Dan Busha

Reputation: 3803

The TabStopIndex value is what indicates which field should receive focus next when the Tab key is pressed. Additionally, you can disable a control's TabStop entirely by setting it through the KeyboardNavigation.IsTabStop attached property.

XAML:

<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
        <DataGrid.Resources>
            <Style x:Key="NoTabStopStyle" TargetType="{x:Type DataGridCell}">
                <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=FirstColumn}" CellStyle="{StaticResource NoTabStopStyle}"/>
            <DataGridTextColumn Binding="{Binding Path=SecondColumn}"/>
            <DataGridTextColumn Binding="{Binding Path=ThirdColumn}" CellStyle="{StaticResource NoTabStopStyle}"/>
        </DataGrid.Columns>
    </DataGrid>

Upvotes: 5

Raj Ranjhan
Raj Ranjhan

Reputation: 3917

You can disable those columns:

<Window.Resources>
    <Style x:Key="NotSelectableColumStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="IsEnabled" Value="False"/>
    </Style>
</Window.Resources>

<Grid Name="LayoutRoot">
     <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn   Binding="{Binding Path=Age}" />
            <!--<Disable this column>-->    
            <DataGridTextColumn   CellStyle="{StaticResource NotSelectableColumStyle}" Binding="{Binding Path=Name}" />
            <DataGridTextColumn  Binding="{Binding Path=Address}" />
        </DataGrid.Columns>
    </DataGrid>
 </Grid>

Upvotes: 0

Related Questions