Wpf依赖属性和附加属性在样式中的应用

本文详细介绍了WPF中如何通过创建依赖属性和附加属性来扩展控件功能,以Button的CornerRadius属性为例,展示了派生类和附加属性的创建过程,以及在样式模板中的应用。依赖属性适用于特定类,而附加属性可以跨类使用,提供了更大的灵活性。同时,文章探讨了两者在触发器和模板设置中的应用场景。
摘要由CSDN通过智能技术生成


前言

在wpf的控件中,有很多属性没有暴露出来给前台设置,比如Button中的边框圆角CornerRadius属性就无法设置,如果直接去样式模板中改的话,那这个样式就太死板了,所以出现了依赖属性和附加属性。在这里我就Button样式中CornerRadius的应用,来解释一下依赖属性和附加属性


一、依赖属性的使用方法

1.添加派生类(以Button为例)

主要添加两个属性
注意:依赖属性的名字必须为普通属性+Property结尾

    public class CornerButton:Button
    {
        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(CornerButton));

    }

2.修改样式模板

主要在Button的外观样式Border中添加绑定
和在指定style的targettype上指定派生类
例如:

        <Style x:Key="ButtonStyle1" TargetType="{x:Type local:CornerButton}">
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CornerButton}">
                        <Border x:Name="border" CornerRadius="{TemplateBinding CornerRadius}" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="true">
                            <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

3.前台应用

先指定类,然后跟普通属性一样的写法

<local:CornerButton CornerRadius="2" Style="{DynamicResource ButtonStyle1}" Height="20" Width="20"/>

二、依赖属性的使用方法

1.添加普通类

如下

    class AttachProperty
    {
        public static CornerRadius GetCornerRadius(DependencyObject obj)
        {
            return (CornerRadius)obj.GetValue(CornerRadiusProperty);
        }

        public static void SetCornerRadius(DependencyObject obj, CornerRadius value)
        {
            obj.SetValue(CornerRadiusProperty, value);
        }

        public static readonly DependencyProperty CornerRadiusProperty =
                DependencyProperty.RegisterAttached("CornerRadius", typeof(CornerRadius), typeof(AttachProperty));

    }

2.修改模板

如下
主要在Border中添加绑定的方式不同

        <Style x:Key="ButtonStyle2" TargetType="{x:Type Button}">
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="border" CornerRadius="{Binding Path=(local:AttachProperty.CornerRadius), RelativeSource={RelativeSource TemplatedParent}}" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="true">
                            <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

3.前台使用

        <Button local:AttachProperty.CornerRadius="2" Style="{DynamicResource ButtonStyle2}" Height="20" Width="20"/>

总结

从本次实验来看,实现同样一种功能,依赖属性的写法似乎简单一点,但是只能给指定类(Button)使用。但是附加属性可以给其他很多控件使用,写法几乎是一样的。但是,如果要做特殊处理,比如在设置属性之后的事件中,做一些其他的事情,这时候,还是用依赖属性比较合适。
注意:在Trigger中,依然支持依赖属性和附加属性,但是在setter中,就无法给其赋值了,目前还没找到方法,等找到了再来填坑。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值