WPF DataTrigger using enum fails

535 views Asked by At

The case:

I have a customized ToggleButton that uses a DrawingImage to draw a hexagon to use as template background making it a hexagonally shaped toggle button.

I want to change the color (Brush) and border (Pen) of the drawing so the button looks different for different purposes.

There’s a couple of conditions that’s taken into consideration when deciding what color should be used, and what I’ve tried so far is having a DependencyProperty and a DataTrigger in the hex button style to select the correct DrawingImage (from a ResourceDictionary). The DependencyProperty is being set to the correct value so that part of the code works the way it should. The problem is that the DataTrigger doesn’t seem to do anything. I'm sure I've missed something silly, I just can't see what it is...

I’ve made a simplified example to demonstrate my problem, and here’s some of the code:

HexButton

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 */
}

Resources

<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

<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>

The IsMouseOver Trigger works like intended. Why doesn't the DataTriggers work?

Note: I haven't tried using an enum before in a binding or trigger, but from what I've seen and understand this should work. At least it compiles and ReSharper doesn't nag!

1

There are 1 answers

5
Mike Eason On BEST ANSWER

You need to use a TemplateBinding instead.

{TemplateBinding CurrentBackground}

In fact, you don't even need to use a DataTrigger. Just use a normal trigger instead.

<Trigger Property="CurrentBackground" ...