• WPF Style Setter use a TemplateBinding?


     <Style TargetType="{x:Type local:ImageButton}">
            <Setter Property="HorizontalContentAlignment"
                    Value="Center"></Setter>
            <Setter Property="VerticalContentAlignment"
                    Value="Center"></Setter>
            <Setter Property="Foreground"
                    Value="White"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:ImageButton}">
                        <Border Background="{TemplateBinding Background}"
                                x:Name="border"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              Margin="{TemplateBinding Padding}"
                                              RecognizesAccessKey="True"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              FocusVisualStyle="{TemplateBinding FocusVisualStyle}" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled"
                                     Value="false">
                                <Setter Property="Foreground"
                                        Value="#ADADAD" />
                            </Trigger>
                            <Trigger Property="IsMouseOver"
                                     Value="True">
                                <Setter Property="Background"
                                        TargetName="border"
                                        Value="{Binding MouseOverBackground, RelativeSource={RelativeSource TemplatedParent}}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
     1  public class ImageButton : Button
     2     {
     3         static ImageButton()
     4         {
     5             DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
     6         }
     7 
     8         public Brush MouseOverBackground
     9         {
    10             get { return (Brush)GetValue(MouseOverBackgroundProperty); }
    11             set { SetValue(MouseOverBackgroundProperty, value); }
    12         }
    13 
    14 
    15         // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    16         public static readonly DependencyProperty MouseOverBackgroundProperty =
    17             DependencyProperty.Register("MouseOverBackground", typeof(Brush), typeof(ImageButton), new PropertyMetadata(Brushes.Transparent));
    18     }
  • 相关阅读:
    Linux基础ls命令
    Linux基础tree命令
    Java银行调度系统
    Java交通灯系统
    Java反射
    Java基础IO流
    Java多线程
    Java集合框架
    Springmvc的一些属性功能
    JS
  • 原文地址:https://www.cnblogs.com/ligl/p/6700921.html
Copyright © 2020-2023  润新知