我看到很多关于如何在DataGrid中为选定行设置样式的示例,例如这个:
我能否禁用选定行的样式?我不想覆盖每一个被选定行所改变的东西。只是不想有任何可见的变化。肯定有比创建模板更容易的方法..
或者..
如果更容易的话,禁用选择行..但从浏览这个论坛来看,那也是hacky的
我看到很多关于如何在DataGrid中为选定行设置样式的示例,例如这个:
我能否禁用选定行的样式?我不想覆盖每一个被选定行所改变的东西。只是不想有任何可见的变化。肯定有比创建模板更容易的方法..
或者..
如果更容易的话,禁用选择行..但从浏览这个论坛来看,那也是hacky的
我找到了可以去掉选择样式的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>
以下方法对我有效:
<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>
我找到了另一种适用于我情况的方法。我将此样式应用于所有单元格,因为我不希望用户选择任何单元格。
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
我先回答第二个问题:要禁用行的选择,您可以更改DataGrid的RowStyle。
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>
然而,这将更改文本样式,因为该行本身现在处于“禁用”状态。它也不能否认用户仍然可以右键单击行以选择它。如果您真的想禁用与数据网格行的任何交互,您可以执行以下操作:
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
</DataGrid.RowStyle>
<!--Other DataGrid items-->
</DataGrid>
由于行仍然启用,文本的样式不会改变。
现在,如果您只想更改选定行的样式而保留功能不变,可以执行以下操作(基本上与@Dan Stevens的答案相同)。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>
需要注意的是,上述解决方案不会改变DataGridRowHeader在选中行时的样式,如下图所示(第一行被选中)。
datagrid.SelectionChanged += (obj, e) =>
Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
datagrid.UnselectAll()));
这将禁用 DataGrid 上的所有选择。
如果您不想完全禁用选择,而只是隐藏它,则需要修改模板。
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="BorderBrush" Value="Transparent"/> <!--Removes brush color change-->
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Foreground}"/> <!--Removes foregound change-->
<Setter Property="Background" Value="Transparent"/> <!--Removes backgound change-->
</Trigger>
</Style.Triggers>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/> <!--Removes dotted border when on cell selection change made by keyboard-->
</Style>
我稍微修改了Dan Stevens和JoshuaTheMiller's的解决方案。如果符合以下条件,请使用此解决方案:
对于像我这样的人,如果有一些具有不同样式的单元格,又不想覆盖所有样式或为每个样式添加触发器,那么这是一个不错的选择:
<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
是带有活动选择的突出边框,而 HighlightTextBrushKey
是具有活动选择的文本颜色。InactiveSelectionHighlightBrushKey
是当选择处于非活动状态时的边框,而 InactiveSelectionHighlightTextBrushKey
是当选择处于非活动状态时的文本。SystemColors
,然后按alt+f12
查看所有定义的内容,并尝试使用看起来可能是它的颜色进行试错,例如任何带有“inactive”的内容... - Benoit Dufresne
<Setter Property="Foreground" Value="Black" />
是不必要的。 - maxp<Setter Property="Foreground" Value="Black" />
是必要的。当它被移除时,非焦点单元格中显示的文本会变成白色并消失在白色背景中。 - Galactic