wpf: DataGrid disable selected row styles - or row selecting

后端 未结 6 1598
野性不改
野性不改 2020-12-03 09:27

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 di

相关标签:
6条回答
  • 2020-12-03 09:56

    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>
    
    0 讨论(0)
  • 2020-12-03 09:56

    This is relatively straightforward:

    datagrid.SelectionChanged += (obj, e) =>
      Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
        datagrid.UnselectAll()));
    

    This disables all selection on the DataGrid.

    If you don't want to disable selection entirely but just hide it you'll need to modify the template.

    0 讨论(0)
  • 2020-12-03 09:59

    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>
    
    0 讨论(0)
  • 2020-12-03 10:19

    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>
    
    0 讨论(0)
  • 2020-12-03 10:22

    I'll answer the second question first: to disable selection of rows, you could change the RowStyle of your DataGrid.

    <DataGrid>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="IsEnabled" Value="False"/>
            </Style>
        </DataGrid.RowStyle>
        <!--Other DataGrid items-->
    </DataGrid>
    

    However, this changes the text style as the row itself is now "disabled". It also doesn't negate the fact that a user can still right click on the row to select it. If you really wanted to disable any kind of interaction with the datagrid's rows, you could do the following:

    <DataGrid>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="IsHitTestVisible" Value="False"/>
            </Style>
        </DataGrid.RowStyle>
        <!--Other DataGrid items-->
    </DataGrid>
    

    As the rows are still enabled, the style of the text does not change.

    Now if you wanted to only change the style of selected row but leave the functionality alone, you could do the following (which is basically the same as @Dan Stevens' answer). The ControlTextBrushKey is the brush that is used by the system to color text items. Please look at this answer for an explanation between DynamicResource and StaticResource.

    <DataGrid>
        <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                    <Trigger Property="DataGridCell.IsSelected" Value="True">
                        <Setter Property="BorderBrush" Value="Transparent"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                        <Setter Property="Background" Value="Transparent"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
        <!--Other DataGrid items-->
    </DataGrid>
    

    It is important to note that the above solution does not change the style of DataGridRowHeader when the row is selected, as can be seen below (the first row is selected).

    0 讨论(0)
  • 2020-12-03 10:23

    For those like me who have some cells with different styles and don't want to override all styles nor add triggers to each style, this is a good bet:

    <DataGrid.Resources>
        <SolidColorBrush 
            x:Key="{x:Static SystemColors.HighlightBrushKey}" 
            Color="#333333"/>
        <SolidColorBrush 
            x:Key="{x:Static SystemColors.HighlightTextBrushKey}" 
            Color="Black"/>
        <SolidColorBrush 
            x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" 
            Color="Black"/>
        <SolidColorBrush 
            x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" 
            Color="Black"/>
    </DataGrid.Resources>
    

    HighlightBrushKey is the highlighted border with active selection and HighlightTextBrushKey is the text color with active selection

    In my case, I want inactive selection to look unselected:

    InactiveSelectionHighlightBrushKey is the border when selection is inactive and InactiveSelectionHighlightTextBrushKey is the text when selection is inactive

    FYI: SystemColors is a static class, part of System.Windows.Media namespace. You can inspect it and shamelessly override any color you don't like!

    0 讨论(0)
提交回复
热议问题