DependencyProperty как значение триггера - PullRequest
0 голосов
/ 03 августа 2020

Я пытаюсь стилизовать свое приложение Button с помощью DependencyProperty

Объявление зависимости:

public static class Button
{
    public static readonly DependencyProperty BackgroundHoverProperty;

    static Button()
    {
        BackgroundHoverProperty = DependencyProperty.RegisterAttached(
            "BackgroundHover",
            typeof(Brush),
            typeof(Button),
            new PropertyMetadata(Application.Current.FindResource("Mask.Hover")));
    }

    public static Brush GetBackgroundHover(DependencyObject obj)
    {
        return (Brush)obj.GetValue(BackgroundHoverProperty);
    }

    public static void SetBackgroundHover(DependencyObject obj, Brush value)
    {
        obj.SetValue(BackgroundHoverProperty, value);
    }
}

Стиль кнопки:

<Style TargetType="Button">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="{StaticResource Border.Default}" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="TextElement.Foreground" Value="{StaticResource Foreground.Default}" />
    <Setter Property="Padding" Value="1" />
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="controls:Button.BackgroundHover" Value="Blue" />
    <Setter Property="Template" Value="{StaticResource ButtonTemplate}" />
</Style>

шаблон кнопки:

<ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
    <Grid>
        <Border
            x:Name="Border"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            SnapsToDevicePixels="True" />
        <ContentPresenter
            x:Name="Content"
            RecognizesAccessKey="True"
            Content="{TemplateBinding Content}"
            ContentTemplate="{TemplateBinding ContentTemplate}"
            ContentStringFormat="{TemplateBinding ContentStringFormat}"
            Margin="{TemplateBinding Padding}"
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
            Focusable="False" />
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter
                Property="Background"
                TargetName="Border"
                Value="{Binding (controls:Button.BackgroundHover), // <--- Here is the binding
                RelativeSource={RelativeSource Self}}" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Я не могу понять, что делаю неправильно.

Изменить: я также пробовал RelativeSource={RelativeSource TemplatedParent} и все еще безрезультатно.

1 Ответ

0 голосов
/ 03 августа 2020

Измените {RelativeSource Self} на {RelativeSource TemplatedParent}:

Value="{Binding (controls:Button.BackgroundHover),
            RelativeSource={RelativeSource TemplatedParent}}"
...