DataGrid 选定的单元格背景
我在使用 WPF DataGrid 时遇到问题。
我有以下代码。
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Name="DataGridCellBorder">
<ContentControl Content="{TemplateBinding Content}">
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBlock
Width="auto"
Height="auto"
Background="Transparent"
Text="{Binding Text}"
TextTrimming="CharacterEllipsis"
TextWrapping="WrapWithOverflow" />
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Orange" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如您所见,我已用自定义模板替换了默认的 DataGridCell。这是需要的,因为如果文本太大而无法适合给定的单元格,我希望单元格具有 TextTrimming(当用户将鼠标悬停在给定的单元格上时,这会完全显示在工具提示中)
我现在的问题是,当我选择单元格,无论我尝试做什么,前景都会设置为白色 - 我真正想要发生的是将单元格(甚至更好的是整个行)背景颜色更改为橙色。
任何有关这方面的帮助都会很棒
克里斯
I'm having trouble with the WPF DataGrid.
I have the following code..
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Name="DataGridCellBorder">
<ContentControl Content="{TemplateBinding Content}">
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBlock
Width="auto"
Height="auto"
Background="Transparent"
Text="{Binding Text}"
TextTrimming="CharacterEllipsis"
TextWrapping="WrapWithOverflow" />
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Orange" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
As you can see, I have replaced the default DataGridCell with a custom template. This was needed as I wanted the cells to have TextTrimming if the text was too big to fit in to the given cell (this is fully shown in the ToolTip when the user hovers over a given cell)
My problem now is that when I select a cell, the foreground get's set to White no matter what I try and do - What I really want to happen is have the cells (or even better, the complete row) background colour change to Orange.
Any help with this would be great
Kris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要更改
DataGridCell.Background
属性,则需要在ControlTemplate
中的某个位置使用它,例如使用
TemplateBinding
If you are going to change the
DataGridCell.Background
property you need to use it somewhere in yourControlTemplate
e.g. using a
TemplateBinding