Gary Barrett
Gary Barrett

Reputation: 2018

How to make WPF Datagrid Column non-focusable?

I have a WPF DataGrid being used for data entry but some DataGridTextColumn are information only and I have set their IsReadOnly="True" so their cells do not go into edit mode. However, they can still receive focus which I want to avoid.

Is there a way to do this?

Upvotes: 6

Views: 5054

Answers (1)

Phil
Phil

Reputation: 42991

Use a cell style and set Focusable=False.

<Page.Resources>
    <Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="Focusable" Value="False"/>
    </Style>
</Page.Resources>

<DataGrid ItemsSource="{Binding Items}" ...>
    <DataGrid.Columns>
        <DataGridTextColumn 
            CellStyle="{StaticResource CellStyle}" 
            IsReadOnly="True" 
            Header="Name" Binding="{Binding Name}"/>

    ....

Upvotes: 12

Related Questions