wpf:DataGrid禁用选定的行样式 - 或行选择

Son*_*oul 42 wpf wpfdatagrid

我看到很多关于如何在DataGrid中为Selected行设置样式的示例,例如:

如何在DataGrid中设置所选行的颜色

我可以禁用选定的行样式吗?我不想要覆盖选择行更改的每一件事.只是不希望任何明显的变化.要比创建模板更简单.

要么..

禁用选择行,如果这更容易..但从浏览这个看似hacky的论坛

禁用在WPF DataGrid中的选择

Son*_*oul 75

想出XAML摆脱选择风格..不理想,但足够接近..

<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>
Run Code Online (Sandbox Code Playgroud)

  • 这可行,但我认为这行:`&lt;Setter Property =“Foreground”Value =“Black”/&gt;`是必要的。 (2认同)

Dan*_*ens 21

这对我有用:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 这对我也有用。接受的答案不起作用。 (3认同)

Bri*_*ink 17

我找到了另一种适合我情况的方法.我为所有单元格设置了这种样式,因为我不希望用户选择任何单元格.

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="IsHitTestVisible" Value="False"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

  • 问题是这对键盘导航没有帮助. (7认同)
  • 请注意,这也会阻止用户单击单元格中的链接和按钮. (4认同)
  • 这也禁用了对列的排序 (4认同)

Jos*_*hua 8

我将首先回答第二个问题:要禁用行的选择,您可以更改DataGrid的RowStyle.

<DataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="IsEnabled" Value="False"/>
        </Style>
    </DataGrid.RowStyle>
    <!--Other DataGrid items-->
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

但是,这会更改文本样式,因为行本身现在已"禁用".它也没有否定用户仍然可以右键单击该行来选择它的事实.如果您真的想要禁用与datagrid行的任何类型的交互,您可以执行以下操作:

<DataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="IsHitTestVisible" Value="False"/>
        </Style>
    </DataGrid.RowStyle>
    <!--Other DataGrid items-->
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

由于行仍处于启用状态,因此文本样式不会更改.

现在,如果你想只改变所选行的风格,但先不谈功能,你可以做以下(这基本上是一样的@丹史蒂文斯的答案).该ControlTextBrushKey是由系统使用以彩色文本项目的画笔.请查看此答案以获取DynamicResource和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>
Run Code Online (Sandbox Code Playgroud)

重要的是要注意,当选择行时,上述解决方案不会更改DataGridRowHeader的样式,如下所示(选择第一行).

在此输入图像描述


Ray*_*rns 6

这是相对简单的:

datagrid.SelectionChanged += (obj, e) =>
  Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
    datagrid.UnselectAll()));
Run Code Online (Sandbox Code Playgroud)

这将禁用DataGrid上的所有选择。

如果您不想完全禁用选择,而只是将其隐藏,则需要修改模板。

  • 你错了。Dispatcher可以在必要时跨线程,但只有在您通过为与您所在的线程不同的线程选择Dispatcher来告知这样做时,才可以这样做。当在这样的UI对象中使用时,Dispatcher.Invoke和Dispatcher.BeginInvoke只需执行该方法-一个同步执行,另一个异步执行。没有其他线程以任何方式,形状或形式参与其中。很高兴我可以为您解决这个问题。我可以看到您为什么对使用Dispatcher.BeginInvoke持怀疑态度,因为您对它的功能有误解。 (2认同)