Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 473
Gary Barrett Avatar asked Apr 05 '12 15:04

Gary Barrett


People also ask

How to make DataGrid column editable in WPF?

By default, you can edit items directly in the DataGrid. The user can enter edit mode in a cell by pressing F2 key or double tapping on a cell. Alternatively, you can set the DataGridColumn. IsReadOnly property to true to disable editing in specific columns of the DataGrid.

What is the difference between grid and DataGrid in WPF?

A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.

Can user add rows DataGrid WPF?

WPF DataGrid (SfDataGrid) provides built-in row called AddNewRow. It allows user to add a new row to underlying collection. You can enable or disable by setting SfDataGrid.


1 Answers

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}"/>

    ....
like image 182
Phil Avatar answered Oct 15 '22 06:10

Phil