IsMouseOverBackground和IsPressedBackground上的TemplateBinding

时间:2014-01-29 09:41:07

标签: wpf xaml

我目前正在学习wpf,并且我已经为App.xaml中的所有按钮创建了一个通用样式。在这种风格中,我为背景指定了一个模板绑定,以便在我的应用程序的特定按钮中修改此属性。

<Style TargetType="{x:Type Button}">
      <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
      ...
      <Setter Property="Template">
        <Setter.Value>
                 <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="bd" Background="{TemplateBinding Background}">
                         ... contentpresenter ...
                    </Border>
 ... end of style ...

现在我想用IsMouseOverBackground和IsPressedBackground做同样的事情,我已经在Trigger中设置了背景值,如下所示:

<ControlTemplate.Triggers>

    <Trigger Property="IsMouseOver" Value="True">
        <Setter TargetName="bd" Property="Background" Value="{StaticResource ButtonHoverBackgroundColor}" />
    </Trigger>

    <Trigger Property="IsPressed" Value="True">
        <Setter TargetName="bd" Property="Background" Value="{StaticResource ButtonPressedBackgroundColor}" />
    </Trigger>

</ControlTemplate.Triggers>

但我不知道如何将TemplateBinding放到按钮的这个特定状态,我想在我的应用程序的特定按钮上设置这些值,而不是每次都创建一个样式。

是否可以在不创建自己的DependencyProperty的情况下执行此操作?('否'是可接受的响应^^)

总结我想要一些简单的事情:

<Button Margin="0,0,0,1" Background="#FF2175A6" IsMouseOverBackground="#FF7021A6" IsPressedBackground="#FF21A639"/>

感谢您的回复;)

1 个答案:

答案 0 :(得分:1)

我认为,您必须使用附加属性来实现您的目标:

您的附加属性的课程:

public static class ButtonBackgrounds
{
    public static readonly DependencyProperty IsMouseOverBackgroundProperty =
        DependencyProperty.RegisterAttached("IsMouseOverBackground", typeof (Brush), typeof (ButtonBackgrounds), new PropertyMetadata(default(Brush)));

    public static void SetIsMouseOverBackground(UIElement element, Brush value)
    {
        element.SetValue(IsMouseOverBackgroundProperty, value);
    }

    public static Brush GetIsMouseOverBackground(UIElement element)
    {
        return (Brush) element.GetValue(IsMouseOverBackgroundProperty);
    }

    public static readonly DependencyProperty IsPressedBackgroundProperty =
        DependencyProperty.RegisterAttached("IsPressedBackground", typeof (Brush), typeof (ButtonBackgrounds), new PropertyMetadata(default(Brush)));

    public static void SetIsPressedBackground(UIElement element, Brush value)
    {
        element.SetValue(IsPressedBackgroundProperty, value);
    }

    public static Brush GetIsPressedBackground(UIElement element)
    {
        return (Brush) element.GetValue(IsPressedBackgroundProperty);
    }
}

然后用于设置背景的XAML将如下所示:

<Button Content="Click Me!!" Margin="10" 
        local:ButtonBackgrounds.IsMouseOverBackground="#FF7021A6" 
        local:ButtonBackgrounds.IsPressedBackground="#FF21A639" />

你的风格将成为:

<Style TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="bd" Background="{TemplateBinding Background}">
                    <ContentPresenter />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="bd" Property="Background" 
                                        Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ButtonBackgrounds.IsMouseOverBackground)}" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="bd" Property="Background" 
                                        Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ButtonBackgrounds.IsPressedBackground)}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

有关附加属性

的更多信息,请参阅此MSDN页面Attached Properties Overview