WPF 依赖属性原理、 附加属性

依赖属性如何节约内存

MSDN中给出了下面几种应用依赖属性的场景:

  1. 希望可在样式中设置属性。

  2. 希望属性支持数据绑定。

  3. 希望可使用动态资源引用设置属性。

  4. 希望从元素树中的父元素自动继承属性值。

  5. 希望属性可进行动画处理。

  6. 希望属性系统在属性系统、环境或用户执行的操作或者读取并使用样式更改了属性以前的值时报告。

  7. 希望使用已建立的、WPF 进程也使用的元数据约定,例如报告更改属性值时是否要求布局系统重新编写元素的可视化对象。

自定义依赖属性

1、声明依赖属性变量。依赖属性的声明都是通过public static来公开一个静态变量,变量的类型必须是DependencyProperty

2、在属性系统中进行注册。

使用DependencyProperty.Register方法来注册依赖属性,

或者是使用DependencyProperty.RegisterReadOnly方法来注册

3、使用.NET属性包装依赖属性

》》》在类型的静态构造函数中通过Register注册 静态 静态 静态

csharp 复制代码
 public partial class UserControl1 : UserControl
 {
     //1、声明依赖属性变量
     public static readonly DependencyProperty MyColorProperty;
     //2、在属性系统中进行注册
     static UserControl1()
     {
        
         MyColorProperty =  DependencyProperty.Register("MyColor", typeof(string), typeof(UserControl1), new PropertyMetadata("Red", 
             
             (s, e) => {
                 var mdp = s as UserControl1;
                 if (mdp != null)
                 {

                     try
                     {
                         var color = (Color)ColorConverter.ConvertFromString(e.NewValue.ToString());
                         mdp.Foreground= new SolidColorBrush(color);
                     }
                     catch (Exception)
                     {

                         mdp.Foreground = new SolidColorBrush(Colors.Yellow);
                     }
                 }
             
             }));

     }
     //3、使用.NET属性包装依赖属性:属性名称与注册时候的名称必须一致,
     public string MyColor
     {
         get { return (string)GetValue(MyColorProperty); }
         set { SetValue(MyColorProperty, value); }
     }
 }
依赖属性的优先级
只读依赖属性
附件属性

IsAttached就是一个附加属性,附加属性没有采用CLR属性进行封装,而是使用静态SetIsAttached方法和GetIsAttached方法来存取IsAttached值。这两个静态方法内部一样是调用SetValue和GetValue来对附加属性读写的。

依赖属性的监听

1.使用DependencyPropertyDescriptor类

2.使用OverrideMetadata的方式。

csharp 复制代码
public class MyTextBox : TextBox
    {
        public MyTextBox()
            : base()
        {
        }

        static MyTextBox()
        {
            //第一种方法,通过OverrideMetadata
            TextProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(new PropertyChangedCallback(TextPropertyChanged)));
        }

        private static void TextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            MessageBox.Show("", "Changed");
        }
    }
csharp 复制代码
public MainWindow()
        {
            InitializeComponent();
            //第二种方法,通过OverrideMetadata
            DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty, typeof(TextBox));
            descriptor.AddValueChanged(tbxEditMe, tbxEditMe_TextChanged);
        }

        private void tbxEditMe_TextChanged(object sender, EventArgs e)
        {
            MessageBox.Show("", "Changed");
        }
附件属性应用 旋转


csharp 复制代码
public  class ManagerRotate:DependencyObject   {


    public static double GetAngle(DependencyObject obj)
    {
        return (double)obj.GetValue(AngleProperty);
    }

    public static void SetAngle(DependencyObject obj, double value)
    {
        obj.SetValue(AngleProperty, value);
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty AngleProperty =
        DependencyProperty.RegisterAttached("Angle", typeof(double), typeof(ManagerRotate), new PropertyMetadata(0.0,OnAngleChanged));

    private static void OnAngleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var element = d as UIElement;
        if (element != null)
        {
            element.RenderTransformOrigin=new Point(0.5,0.5);
            element.RenderTransform = new RotateTransform((double)e.NewValue);
        }
    }
}
相关推荐
hez201014 小时前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
追逐时光者1 天前
.NET Fiddle:一个方便易用的在线.NET代码编辑工具
后端·.net
mudtools1 天前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫1 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
追逐时光者2 天前
精选 4 款基于 .NET 开源、功能强大的 Windows 系统优化工具
后端·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
唐青枫2 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
私人珍藏库2 天前
[Windows] 微软 .Net 运行库离线安装包 | Microsoft .Net Packages AIO_v09.09.25
microsoft·.net·运行库
追逐时光者3 天前
C#/.NET/.NET Core技术前沿周刊 | 第 54 期(2025年9.8-9.14)
后端·.net
追逐时光者3 天前
C#/.NET/.NET Core编程技巧练习集,配套详细的文章教程讲解!
后端·.net