0

Context

  • I made a custom control called ButtonPath which is derived from the default Button to allow me to easily add Buttons with different PathGeometry Figures and SolidColorBrush from the application's ResourceDictionary

  • To achieve said goal, ButtonPath has two DependencyProperties:

    • PathGeometry, registered name = Path
    • SolidColorBrush, registered name = Fill
  • This custom button works as intended in the relevant XAML files when it is placed As Is, e.g. in a UserControl:

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

  • As showcased in the xaml snippets below, if the ToggleTimerCommand is executed, the button's Fill does not change while its Height does.

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Clemens thanks for pointing the issue with the Fill assignment in the ControlTemplate, i intentionally changed it (had it just like yours) thinking it was possible to have a default assigned value for the 'Fill' in the Template yet still retain the capabilities of the 'Fill' to be a 'Setter' for 'DataTrigger', which i guess would be my follow up question - is it impossible to set it as such? moreover, is that the right way of thinking of how to do it? also, thanks for your suggestion and explanation on the general types usage, i'll do some more research on it later
You would define a default value in a Setter in the ButtonPath Style: <Setter Property="Fill" Value="{StaticResource Brush.Dark.Foreground01.Static}"/>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.