wpfmvvmdependency-propertiesdatatriggerwpf-style

DependencyProperty as Setter for Style Trigger


Context

<UserControl ... xmlns:customControls="...">
    <Grid>
        <customControls:ButtonPath
            Path="{StaticResource Record}"
            Fill="{StaticResource Brush.Dark.Foreground01.Static}"/>
    </Grid>
</UserControl>

Issue

Applying the Fill DependencyProperty as a Style Setter for DataTrigger does nothing, while other properties work just fine, e.g.:

I've looked at other similar questions here but couldn't find the answer that I'm looking for and my previous tests returned no errors, so if someone could point me in the right direction, it'd be greatly appreciated.

Thanks in advance.


xaml snippets

The button within a UserControl

<UserControl ... xmlns:customControls="...">
    <Grid>
        <customControls:ButtonPath
            Command="{Binding ToggleTimerCommand}"
            Path="{StaticResource Record}"
            Style="{StaticResource buttonStyle_Record}"/>
    </Grid>
</UserControl>

Relevant Style

<ResourceDictionary ... xmlns:customControls="...">
    <Style
        x:Key="buttonStyle_Record"
        BasedOn="{StaticResource {x:Type customControls:ButtonPath}}"
        TargetType="customControls:ButtonPath">
        <Style.Triggers>
            <DataTrigger Binding="{Binding TimerActive}" Value="true">
                <Setter Property="Fill" Value="Red" />
                <Setter Property="Height" Value="25" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

The ButtonPath Template

<ResourceDictionary... xmlns:customControls="...">
    <Style TargetType="{x:Type customControls:ButtonPath}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type customControls:ButtonPath}">
                    <Border
                        Background="Transparent"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="0">
                        <Viewbox Width="18" Height="18">
                            <Path
                                x:Name="icon"
                                Data="{TemplateBinding Path}"
                                Fill="{StaticResource Brush.Dark.Foreground01.Static}"
                                Stretch="Fill" />
                        </Viewbox>
                    </Border>
                    <ControlTemplate.Triggers>
                        <!--some common triggers-->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Solution

  • The Fill assignment in the ControlTemplate is wrong.

    <Path x:Name="icon"
        Data="{TemplateBinding Path}"
        Fill="{StaticResource Brush.Dark.Foreground01.Static}"
        Stretch="Fill" />
    

    It should be

    <Path x:Name="icon"
        Data="{TemplateBinding Path}"
        Fill="{TemplateBinding Fill}"
        Stretch="Fill" />
    

    As a note, there is no point in declaring your custom dependencies as PathGeometry and SolidColorBrush. You should be using the more general types Geometry and Brush.