Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf: DataGrid disable selected row styles - or row selecting

I am seeing a lot of examples on how to style Selected rows in DataGrid such as this one:

How can I set the color of a selected row in DataGrid

Can i just disabled selected row styling? i don't want to have to override every single thing that selected row changes. Just don't want any visible changes. Gotta be easier way than to create templates..

or..

disable selecting rows, if that is easier.. but from browsing this forum that seems hacky as well

Disable selecting in WPF DataGrid

like image 703
Sonic Soul Avatar asked Jun 15 '10 16:06

Sonic Soul


People also ask

How to disable row selection in DataGrid WPF?

If the value IsEnable = true, the Row on DataGrid is enabled. If IsEnable = false, the Row on the DataGrid is disabled.

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.

What is DataGrid WPF?

A DataGrid is a control that displays data in a customizable grid. It provides a flexible way to display a collection of data in rows and columns. The hierarchical inheritance of DataGrid class is as follows −


3 Answers

figured out the XAML to get rid of selection style.. not ideal, but close enough..

<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="Foreground" Value="Black" />
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="{x:Null}" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
        </Trigger>
    </Style.Triggers>
</Style>
like image 88
Sonic Soul Avatar answered Oct 26 '22 22:10

Sonic Soul


Here's what worked for me:

<DataGrid>
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger Property="DataGridCell.IsSelected" Value="True">
                    <Setter Property="BorderBrush">
                        <Setter.Value>
                            <SolidColorBrush Color="Transparent"/>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Foreground"
                            Value="{DynamicResource
                                   {x:Static SystemColors.ControlTextBrushKey}}"/>
                    <Setter Property="Background">
                        <Setter.Value>
                            <SolidColorBrush Color="Transparent"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>
    <!-- ... -->
</DataGrid>
like image 35
Dan Stevens Avatar answered Oct 26 '22 21:10

Dan Stevens


I found another way that works well for my situation. I set this style for all cells because I don't want the user to select any cells.

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="IsHitTestVisible" Value="False"/>
</Style>
like image 20
Brian Ensink Avatar answered Oct 26 '22 21:10

Brian Ensink