wpf:DataGrid 禁用选定的行样式 - 或行选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3046988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wpf: DataGrid disable selected row styles - or row selecting
提问by Sonic Soul
I am seeing a lot of examples on how to style Selected rows in DataGrid such as this one:
我看到了很多关于如何在 DataGrid 中设置选定行样式的示例,例如:
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
禁用选择行,如果这更容易...但浏览这个似乎也很hacky的论坛
回答by Sonic Soul
figured out the XAML to get rid of selection style.. not ideal, but close enough..
想出 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>
回答by Dan Stevens
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>
回答by Brian Ensink
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>
回答by Joshua
I'll answer the second question first: to disable selection of rows, you could change the RowStyle of your DataGrid.
我将首先回答第二个问题:要禁用行选择,您可以更改 DataGrid 的 RowStyle。
<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 onlychange 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 ControlTextBrushKeyis the brush that is used by the system to color text items. Please look at this answerfor an explanation between DynamicResource and StaticResource.
现在,如果你想只改变所选行的风格,但先不谈功能,你可以做以下(这基本上是一样的@丹史蒂文斯的答案)。该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>
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).
需要注意的是,上面的解决方案在选择行时并没有改变DataGridRowHeader的样式,如下所示(选择了第一行)。
回答by Ray Burns
This is relatively straightforward:
这是相对简单的:
datagrid.SelectionChanged += (obj, e) =>
Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
datagrid.UnselectAll()));
This disables all selection on the DataGrid.
这将禁用 DataGrid 上的所有选择。
If you don't want to disable selection entirely but just hide it you'll need to modify the template.
如果您不想完全禁用选择而只是隐藏它,则需要修改模板。
回答by Benoit Dufresne
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
HighlightBrushKey
是带有活动选择的突出显示的边框,是带有活动选择HighlightTextBrushKey
的文本颜色
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
InactiveSelectionHighlightBrushKey
是选择处于非活动状态时的边框,InactiveSelectionHighlightTextBrushKey
是选择处于非活动状态时的文本
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!
仅供参考:SystemColors 是一个静态类,是 System.Windows.Media 命名空间的一部分。您可以检查它并无耻地覆盖任何您不喜欢的颜色!