使用枚举的 WPF DataTrigger 失败

WPF DataTrigger using enum fails

案例:

我有一个自定义的 ToggleButton,它使用 DrawingImage 绘制六边形作为模板背景,使其成为六边形切换按钮。

我想更改绘图的颜色 (Brush) 和边框 (Pen),使按钮看起来因不同目的而有所不同。

在决定应该使用什么颜色时要考虑几个条件,到目前为止我尝试的是在十六进制按钮样式中使用 DependencyPropertyDataTrigger到 select 正确的 DrawingImage(来自 ResourceDictionary)。 DependencyProperty 被设置为正确的值,以便部分代码按应有的方式工作。问题是 DataTrigger 似乎什么也没做。我确定我错过了一些愚蠢的东西,我只是看不到它是什么...

我做了一个简化的例子来演示我的问题,下面是一些代码:

六角按钮

public class HexButton : ToggleButton
{
    public enum HexBackground
    {
        Default,
        Red,
        RedHover,
        RedChecked,
        RedCheckedHover,
        // etc...
    }

    public HexBackground CurrentBackground
    {
        get { return (HexBackground)GetValue(CurrentBackgroundProperty); }
        set { SetValue(CurrentBackgroundProperty, value); }
    }

    public static readonly DependencyProperty CurrentBackgroundProperty = DependencyProperty.Register(
        "CurrentBackground",
        typeof(HexBackground),
        typeof(HexButton),
        new PropertyMetadata(HexBackground.Default)
        );

    /* removed some code to avoid too much wall of text */
}

资源

<DrawingImage x:Key="HexRed">
    <DrawingImage.Drawing>
        <DrawingGroup>
            <GeometryDrawing Brush="Red" Geometry="M 25,0 L 75,0 L 100,43 L 75,86 L 25,86 L 0,43 Z">
                <GeometryDrawing.Pen>
                    <Pen Brush="Black" Thickness="2" LineJoin="Round"/>
                </GeometryDrawing.Pen>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>
<DrawingImage x:Key="HexRedHover">
    <DrawingImage.Drawing>
        <DrawingGroup>
            <GeometryDrawing Brush="DarkRed" Geometry="M 25,0 L 75,0 L 100,43 L 75,86 L 25,86 L 0,43 Z">
                <GeometryDrawing.Pen>
                    <Pen Brush="Black" Thickness="6" LineJoin="Round"/>
                </GeometryDrawing.Pen>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

风格

<Style x:Key="HexButtonStyle" TargetType="{x:Type hex:HexButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type hex:HexButton}">
                <Grid>
                    <Image x:Name="img" Source="{StaticResource HexDefault}"/>
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=CurrentBackground}" Value="{x:Static hex:HexButton+HexBackground.Red}">
                        <Setter TargetName="img" Property="Source" Value="{StaticResource HexRed}"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=CurrentBackground}" Value="{x:Static hex:HexButton+HexBackground.RedHover}">
                        <Setter TargetName="img" Property="Source" Value="{StaticResource HexRedHover}"></Setter>
                    </DataTrigger>
                    <!--<Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="img" Property="Source" Value="{StaticResource HexRedHover}"/>
                    </Trigger>-->
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

IsMouseOver 触发器按预期工作。为什么 DataTriggers 不起作用?

注意:我以前没有尝试过在绑定或触发器中使用枚举,但据我所见和理解,这应该可行。至少它可以编译并且 ReSharper 不会唠叨!

您需要改用 TemplateBinding

{TemplateBinding CurrentBackground}

事实上,您甚至不需要使用 DataTrigger。只需使用普通触发器即可。

<Trigger Property="CurrentBackground" ...