如何阻止DataGrid行颜色的变化?

编程入门 行业动态 更新时间:2024-10-27 19:15:36
本文介绍了如何阻止DataGrid行颜色的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在WPF DataGrid中选择一行时,我已经实现了代码来解决双击问题。我正在使用以下代码: stackoverflow/a/5857908/40106 。

I've implemented code to get around the double click issue when selecting a row in a WPF DataGrid. I'm using the following code from here: stackoverflow/a/5857908/40106.

<Style TargetType="DataGridCell"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="IsEditing" Value="True" /> </Trigger> </Style.Triggers> </Style>

行有交替的颜色。问题是当我将鼠标悬停在一列,在一列中,浅蓝色被白色代替。

Rows have alternating colors. The problem is when I mouse over a row, in one column, the light blue color is replaced by white.

除了这个问题以外,上述代码非常有用。

The above code works great except for this one issue. How do I stop the color from changing when mousing over a row?

我尝试过以下操作,但没有任何影响:

I have tried the following but it doesn't have any effect:

<Style TargetType="DataGridCell"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="IsEditing" Value="True" /> <Setter Property="Background" Value"AliceBlue" /> </Trigger> </Style.Triggers> </Style>

推荐答案

问题是,单元格将显示它的编辑

The problem is, that the cell will display it's edit style when you hover the mouse above the cell.

对于 DataGridTextColumn 这意味着,一个 TextBox 显示白色背景。

For a DataGridTextColumn this means, that a TextBox with a white background is displayed.

您可以将样式设置为< DataGridTextColumn.EditingElementStyle> 并将背景设置为透明。

You can set a Style to <DataGridTextColumn.EditingElementStyle> and set the Background to transparent.

<DataGridTextColumn Header="Name" Binding="{Binding Name}" > <DataGridTextColumn.EditingElementStyle> <Style TargetType="TextBox"> <Setter Property="Background" Value="Transparent"></Setter> <Setter Property="BorderThickness" Value="0"></Setter> </Style> </DataGridTextColumn.EditingElementStyle> </DataGridTextColumn>

<

To get the white Background, when actually editing the cell you could add another trigger to the IsSelected event:

<Style TargetType="DataGridCell"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="IsEditing" Value="True" /> </Trigger> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="White"/> </Trigger> </Style.Triggers> </Style>

另一个选择是将DataGridCell样式应用于仅限CheckBoxColumns。对于其他列类型,它不会有什么不同。

Another option would be to apply the DataGridCell Style to CheckBoxColumns only. For other Column types it wouldn't make a difference anyway.

更多推荐

如何阻止DataGrid行颜色的变化?

本文发布于:2023-11-11 10:11:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!