WPF依赖属性和附加属性程序实现

本概述文章介绍 WPF 依赖属性和附件属性如何在 XAML 和代码中实现。

详细的说明可以查看微软文档:依赖属性概述 - WPF .NET | Microsoft Learn了解 WPF 属性系统和依赖属性的功能,依赖属性是由 WPF 属性系统提供支持的属性。icon-default.png?t=O83Ahttps://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/properties/dependency-properties-overview?view=netdesktop-8.0

1.依赖属性

在CS文件打出propdp+Tab会自动生成一个依赖属性代码

定义依赖属性+回调实现

   public class CustomControl_Button:DependencyObject
   {
       public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
       "Text",
       typeof(string),
       typeof(CustomControl_Button),
       new PropertyMetadata(defaultValue: "button", OnText));

       private static void OnText(DependencyObject d, DependencyPropertyChangedEventArgs e)
       {
           System.Windows.Controls.Button button = d as System.Windows.Controls.Button;
           button.Content = e.NewValue;
       }

       [Category("自定义属性"), Description("Text"), DisplayName("Text")]
       public string Text
       {
           get { return (string)GetValue(TextProperty); }
           set { SetValue(TextProperty, value); }
       }
    }
  <local:CustomControl_Button local:CustomControl_Button.Text="123"/>

定义依赖属性+数据绑定实现

    public class CustomControl_Button:DependencyObject
    {
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        "Text",
        typeof(string),
        typeof(CustomControl_Button),
        new PropertyMetadata(defaultValue: "button"));

        [Category("自定义属性"), Description("Text"), DisplayName("Text")]
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
    }
  <local:CustomControl_Button  local:CustomControl_Button.Text="123"
  Content= "{Binding  RelativeSource={RelativeSource Self},Path=(local:CustomControl_Button.Text)}" />

2.附加属性

在CS文件打出propa+Tab会自动生成一个附加属性代码

定义附加属性+回调实现

public class CustomControl_ButtonRegister
{
    public static string  GetMyProperty(DependencyObject obj)
    {
        return (string)obj.GetValue(MyPropertyProperty);
    }

    public static void SetMyProperty(DependencyObject obj, string value)
    {
        obj.SetValue(MyPropertyProperty, value);
    }
 
    public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached(
    "MyProperty",
    typeof(string), 
    typeof(CustomControl_ButtonRegister),
    new PropertyMetadata(defaultValue:"button", OnMyProperty));

    private static void OnMyProperty(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        System.Windows.Controls.Button button = d as System.Windows.Controls.Button;
        button.Content = e.NewValue;    
    }
}
   <local:CustomControl_Button local:CustomControl_ButtonRegister.MyProperty="12"/>

定义注册属性+数据绑定实现

  public class CustomControl_ButtonRegister
  {
      public static string  GetMyProperty(DependencyObject obj)
      {
          return (string)obj.GetValue(MyPropertyProperty);
      }

      public static void SetMyProperty(DependencyObject obj, string value)
      {
          obj.SetValue(MyPropertyProperty, value);
      }

      public static readonly DependencyProperty MyPropertyProperty =DependencyProperty.RegisterAttached(
      "MyProperty",
      typeof(string), 
      typeof(CustomControl_ButtonRegister),
      new PropertyMetadata(defaultValue:"button"));
  }
   <local:CustomControl_Button local:CustomControl_ButtonRegister.MyProperty="111"
   Content= "{Binding  RelativeSource={RelativeSource Self},Path=(local:CustomControl_ButtonRegister.MyProperty)}" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值