0

I have the following control template defined as a resource in my user control:

<UserControl.Resources>
<ControlTemplate x:Key="NotificationToggleButtonTemplate" TargetType="{x:Type wpfSupport:NotificationToggleButton}">
    <Border Name="Border" CornerRadius="15" Padding="2" BorderThickness="2">
        <dxlc:LayoutItem HorizontalAlignment="Center" VerticalAlignment="Center">
            <Label FontWeight="Bold" FontSize="12" Padding="0" Margin="0"
                   Content="{Binding 
                Path=GetCounters.PendingDocumentRequests,Mode=OneWay}" />
        </dxlc:LayoutItem>
    </Border>
    <ControlTemplate.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsChecked" Value="False"/>
                <Condition Property="IsMouseOver" Value="False"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent},
                Path=(wpfSupport:NotificationToggleButton.ActiveButtonColour)}" />
            <Setter Property="BorderBrush" TargetName="Border" Value="Transparent"/>
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

I have created a ToggleButton descendant:

    public class NotificationToggleButton : ToggleButton
{
    public static readonly DependencyProperty ActiveButtonColourProperty =
        DependencyProperty.RegisterAttached("ActiveButtonColour", typeof(Brush), typeof(NotificationToggleButton),
            new FrameworkPropertyMetadata(Brushes.Black));

    public static Brush GetActiveButtonColour(DependencyObject target) => (Brush) target.GetValue(ActiveButtonColourProperty);

    public static void SetActiveButtonColour(DependencyObject target, Brush value) => target.SetValue(ActiveButtonColourProperty, value);
}

And I use the new control here:

            <wpfSupport:NotificationToggleButton Command="{Binding PendingFilterButtonCommand}" IsChecked="{Binding PendingFilterSelected}"
                      Padding="0" ToolTip="{extensions:Translate ToolTipNotificationPendingDocumentRequest}"
                      Opacity="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, 
            Converter={StaticResource EnabledToOpacityConverter}}" ActiveButtonColour="#4CAF50" 
                      Template="{StaticResource NotificationToggleButtonTemplate}"/>

But the background is never set when that trigger matches. This is an update to code that works fine but which uses multiple control templates (so I'm trying to reduce code duplication). If I change the trigger Background setter to a hard coded colour it works as expected. I also don't get binding errors. So it seems as if the dependency property isn't returning the correct value.

Andrue Cope
  • 249
  • 3
  • 8
  • Why is ActiveButtonColour an attached property, and not a regular dependency property? A property of type Brush should also not be called "Colour". – Clemens Jun 08 '21 at 09:30
  • 1
    Besides that, your code works with `Value="{Binding RelativeSource={RelativeSource Self}, Path=(wpfSupport:NotificationToggleButton.ActiveButtonColour)}"`. There must however also be an element in the ControlTemplate that actually uses the Background property, like `` – Clemens Jun 08 '21 at 10:57
  • Does it? I thought I'd tried that previously. Unfortunate Visual Studio is playing up making it hard to test anything. Might have to bounce it. – Andrue Cope Jun 08 '21 at 11:01
  • Ah ha! Thank you. I was missing the '{TemplateBinding Background}' bit. Now I can look at tidying the code and as you suggest making it a simple property. – Andrue Cope Jun 08 '21 at 11:05
  • But now I can't add that as a solution because some 'kind soul' closed my question claiming that it was a duplicate of another one. Supposedly I have to create my question all over again just so I can post the solution. – Andrue Cope Jun 08 '21 at 11:09
  • Don't worry. You're not the first one who had this problem. There are already quite a few answers covering either the wrong RelativeSource or the missing TemplateBinding. – Clemens Jun 08 '21 at 11:17

0 Answers0