WPF中的依赖属性

昨天学习了下WPF的以来属性,记录下自己的理解。

我们一般给一个类设置一个属性很简单,但是如果给一个控件设置一个属性,会比较麻烦。

比如说,自己做一个button控件,继承自button

 1     class MyButton : Button
 2     {
 3         
 4         private Color _backColor;
 5         public Color BackColor
 6         {
 7             get
 8             {
 9                 return _backColor;
10             }
11             set
12             {
13                 _backColor = value;
14                 this.Background = new SolidColorBrush() { Color = _backColor };
15             }
16         }
17     }

这个属性目的是设置按钮的背景色。这没有什么问题,但是在设置样式的时候会出问题。

1     <phone:PhoneApplicationPage.Resources>
2         <Color x:Key="MyForceColor">Red</Color>
3         <Color x:Key="MyBackColor">White</Color>
4         <Style x:Name="mystyle" TargetType="PhoneApp3:MyButton">
5             <Setter Property="BackColor" Value="Red"/>
6         </Style>

 这个时候会出现编译错误,意思就是说不能够在style里面直接设置自定义属性。

如果想在style里面配置那么就需要使用以来属性。

 1     public class MyButton : Button
 2     {
 3         public static readonly DependencyProperty ForceColorProperty =
 4             DependencyProperty.Register("ForceColor",
 5             typeof(Color),
 6             typeof(MyButton),
 7             new PropertyMetadata(Colors.Black, OnColorChanged));
 8 
 9         public static readonly DependencyProperty BackColorProperty =
10             DependencyProperty.Register("BackColor",
11             typeof(Color),
12             typeof(MyButton),
13             new PropertyMetadata(Colors.White, OnColorChanged));
14 
15         public Color ForceColor
16         {
17             set { SetValue(ForceColorProperty, value); }
18             get { return (Color)GetValue(ForceColorProperty); }
19         }
20         public Color BackColor
21         {
22             set { SetValue(BackColorProperty, value); }
23             get { return (Color)GetValue(BackColorProperty); }
24         }
25 
26         static void OnColorChanged(DependencyObject obj,
27             DependencyPropertyChangedEventArgs args)
28         {
29             var btn = obj as MyButton;
30             if (args.Property == ForceColorProperty)
31             {
32                 btn.Foreground = new SolidColorBrush() { Color = (Color)args.NewValue };
33             }
34             if (args.Property == BackColorProperty)
35             {
36                 btn.Background = new SolidColorBrush() { Color = (Color)args.NewValue };
37             }
38         }
39 
40 
41 
42         
43 
44     }

很坑爹啊,有木有,很复杂啊,有不有,这他妈谁能记得住啊。

不过微软给了一个快捷键来方便的生成模版,只要你输入”prodp“,然后连续按两次tab键,就可以自动生成一个模版。

1         public int MyProperty
2         {
3             get { return (int)GetValue(MyPropertyProperty); }
4             set { SetValue(MyPropertyProperty, value); }
5         }
6 
7         // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
8         public static readonly DependencyProperty MyPropertyProperty =
9             DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));

我们可以来看看注册函数

DependencyProperty.Register(
"MyProperty", -- 名字
typeof(int), --该属性的类型
typeof(ownerclass), --该属性所属类的类型
new PropertyMetadata(0));--属性变化以后的处理

明白意思了也不是很复杂了,不过要记住处理函数的格式

  static void OnColorChanged(DependencyObject obj,DependencyPropertyChangedEventArgs args)

而且别忘了要是静态的。

 最后还有一点,因为属性是静态的,也就是说它是全局一份的,所以在处理的时候要很小心。

 1         static void OnColorChanged(DependencyObject obj,
 2             DependencyPropertyChangedEventArgs args)
 3         {
 4             var btn = obj as MyButton;
 5             if (args.Property == ForceColorProperty)
 6             {
 7                 btn.Foreground = new SolidColorBrush() { Color = (Color)args.NewValue };
 8             }
 9             if (args.Property == BackColorProperty)
10             {
11                 btn.Background = new SolidColorBrush() { Color = (Color)args.NewValue };
12             }
13         }

注意第四行,这句可以保证是的我改变的设置仅仅是当前对象的设置。而不是全局的。

原文地址:https://www.cnblogs.com/HighFun/p/2842008.html