首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TemplateBinding on IsMouseOverBackground和IsPressedBackground

TemplateBinding on IsMouseOverBackground和IsPressedBackground
EN

Stack Overflow用户
提问于 2014-01-29 09:41:07
回答 1查看 276关注 0票数 1

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

代码语言:javascript
复制
<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做同样的事情,我已经在触发器中设置了背景值,如下所示:

代码语言:javascript
复制
<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的情况下做到这一点? ('No‘是可接受的响应^^)

,总结一下,我想要一些像这样简单的东西:

代码语言:javascript
复制
<Button Margin="0,0,0,1" Background="#FF2175A6" IsMouseOverBackground="#FF7021A6" IsPressedBackground="#FF21A639"/>

(谢谢你的答复;)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-29 12:26:02

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

具有附加属性的类:

代码语言:javascript
复制
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如下所示:

代码语言:javascript
复制
<Button Content="Click Me!!" Margin="10" 
        local:ButtonBackgrounds.IsMouseOverBackground="#FF7021A6" 
        local:ButtonBackgrounds.IsPressedBackground="#FF21A639" />

你的风格会变成:

代码语言:javascript
复制
<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页,附件属性概述

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21427175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档