在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行颜色的变化?
发布评论