为什么下面的IsSelected触发器仅在代码中没有模板 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>

最佳答案

当重新模板化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>

关于wpf - DataGridCell.Template样式覆盖IsSelected触发器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4201896/

10-12 06:45