为什么下面的IsSelected Trigger仅在代码中没有Template Setter时才起作用?如何使两者都按预期运行?
我的Application.Resources标签中有以下内容;
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<ContentPresenter VerticalAlignment="Center"></ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:12)
当您重新模板化DataGridCell时,您也会丢失实际上是绘制背景的控件的边框。添加它,它会工作。
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>