DataGrid 处于非活动状态时选择的行颜色
当 DataGrid 失去焦点时,如何设置 WPF DataGrid 样式以更改所选行的颜色?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
当 DataGrid 失去焦点时,如何设置 WPF DataGrid 样式以更改所选行的颜色?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(12)
经过多年的搜索,我发现了一种令人惊讶的简单方法,比之前发布的 Got/LostFocus 方法更清晰:
这只是将非活动背景颜色设置为 DarkGray,将活动背景颜色保留为默认值,但您也可以更改它当然也使用 SystemColors.HighlightBrushKey 。
非活动选择的前台资源键是 SystemColors.InactiveSelectionHighlightTextBrushKey。
After ages of searching, I found a surprisingly simple way to do this that's cleaner than the Got/LostFocus approach posted earlier:
This just sets the inactive background colour to DarkGray, leaving the active background colour to the default, but you can change that too using the SystemColors.HighlightBrushKey too of course.
The foreground resource key for inactive selections is SystemColors.InactiveSelectionHighlightTextBrushKey.
适用于 4.0 的完整解决方案。请注意,这在 CellStyle 上。
Complete solution that works for 4.0. Note that this on the CellStyle.
这样做:
Do it like this:
这些答案都没有给我我想要的东西。 Steve Streeting 评价最高的答案更改了我不想更改的数据网格的其他部分,而其他答案没有提供非活动颜色更改,而是仅正确定位了该行。因此,这里是他们的答案的混合,这些答案改变了非活动颜色,仅在行上,而不是在网格的其他位置。
None of these answers gave me quite what I was looking for. The top rated by Steve Streeting changed other sections of the datagrid that I didn't want to change, and other answers weren't providing the inactive color change, but were properly targeting the row only. So here's a mixture of their answers that changes the inactive color, only on the rows and not in other places on the grid.
最新答案:
这适用于.Net 4.0,并且您不必对颜色进行硬编码:
LATE ANSWER:
This works in .Net 4.0, and you don't have to hardcode the color:
对于 .Net Framework 4.0(或者如果您不想使用 InactiveSelection... 画笔键):
创建一个 DataGridRow 样式/控件模板,并添加以下触发器:
For .Net Framework 4.0 (or if you don't want to use the InactiveSelection... brush keys):
Create a DataGridRow style/control template, and add these triggers:
我将其添加到我的 ResourceDictionary 中,以便它适用于我的程序中的所有数据网格。
I added this to my ResourceDictionary so that it applies to all data grids in my program.
您应该在 DataGrid 中定义“DataGrid.CellStyle”部分,如下所示:
You should define section "DataGrid.CellStyle" inside your DataGrid like this:
自己找答案吧。
将画笔添加到 DataGrid 的资源中,该画笔可以从后面的代码更改其“Color”属性,并对其引用 HighlightBrushKey:
然后添加 DataGrids 事件处理程序以手动更改颜色:
Find an answer by myself.
Add to DataGrid's resources the brush, which can change its 'Color' property from code behind, and reference HighlightBrushKey to it:
Then add DataGrids event handlers to manually change the color:
对于 .net Framework 4.0
https://social.msdn.microsoft.com/Forums/vstudio/en-US/635642e6-4808-4b3e-8aea-c8c434397d0f/datagrid-lost-focus-brush?forum=wpf
For .net Framework 4.0
https://social.msdn.microsoft.com/Forums/vstudio/en-US/635642e6-4808-4b3e-8aea-c8c434397d0f/datagrid-lost-focus-brush?forum=wpf
对于.NET 4.0或更高版本:还可以以编程方式设置颜色:
对于.NET 4.5或更高版本,有以下替代方案以编程方式设置颜色:
For .NET 4.0 or higher: It is also possible to set the colors programmatically:
For .NET 4.5 or higher there is the following alternative to set the colors programmatically:
我有类似的问题。
该场景是一个 DataGrid,其颜色分配给:
行背景和交替行背景。
当 DataGrid 失去焦点时,所选行将变为灰色,当 DataGrid 通过单击任何行重新获得焦点时,将返回到正确的颜色。
这种效果是令人不愉快的。
我从 Steve Streeting 此处提出的解决方案开始:
但是通过更改 Color =“Transparent”。
这以简单且令人满意的方式解决了问题。
对
.net 4.5
有效,不适用于.net 4.0
。希望这个解决方案是有用的
不正确的行为:
正确的行为:
I had a similar problem.
The scenario was a DataGrid with colors assigned for:
RowBackground and AlternatingRowBackground.
When the DataGrid Lose Focus, the selected row became Gray, returning to the correct color when the DataGrid regained Focus by clicking on any row.
This effect was unpleasant.
I started from the solution proposed here by Steve Streeting:
but by changing Color = "Transparent".
this solved the problem in a simple and satisfactory way.
valid for
.net 4.5
, not for.net 4.0
.Hope this solution is useful
incorrect behavior:
correct behavior: