参考链接

《WPF编程宝典》4.1.1 & 4.1.2 & 4.1.3
《深入浅出WPF》7.2.2

什么是依赖属性

可以通过启用原本是公共语言运行时 (CLR) 属性的属性来支持样式设置、数据绑定、继承、动画和默认值,方法是将该属性作为依赖属性进行实现。 依赖属性是通过调用 Register 方法(或 RegisterReadOnly)在 WPF 属性系统中注册,并且受 DependencyProperty 标识符字段支持的属性。 只有 DependencyObject 类型可以使用依赖属性,但是 DependencyObject 在 WPF 类层次结构中级别很高,因此 WPF 中大部分可用的类都支持依赖属性。 若要详细了解依赖属性和此 SDK 中对依赖属性进行描述所使用的一些术语和约定,请参阅依赖属性概述

应用场景

在类上实现属性时,只要类派生自 DependencyObject,就可以选择使用 DependencyProperty 标识符支持属性,从而使其成为依赖属性。 不必总是将属性实现为依赖属性,这不一定合适,具体取决于方案需要。 有时,使用私有字段支持属性的通常方法已足够满足需求。 但是,如果要使属性支持以下一个或多个 WPF 功能,则应该将属性实现为依赖属性:

  • 需要可以在样式中设置属性。 有关详细信息,请参阅样式设置和模板化
  • 需要属性支持数据绑定。 有关数据绑定依赖属性的详细信息,请参阅绑定两个控件的属性
  • 需要可以使用动态资源引用设置属性。 有关详细信息,请参阅 XAML 资源
  • 需要从元素树中的父元素自动继承属性值。 这种情况下,即使为 CLR 访问也创建了属性包装器,也应该使用 RegisterAttached 方法注册。 有关详细信息,请参阅属性值继承
  • 需要属性可以进行动画处理。 有关详细信息,请参阅 动画概述
  • 需要属性系统在先前的值因属性系统、环境或用户执行的操作而发生更改,或者因读取和使用样式而发生更改时进行报告。 通过使用属性元素据,属性可以指定回调方法,每次属性系统确定属性值已明确改动时将调用此回调方法。 与此相关的一个概念是属性值强制转换。 有关详细信息,请参阅依赖属性回调和验证
  • 需要使用同时也被 WPF 进程使用的已建立的元数据约定,例如报告更改属性值是否需要布局系统重新安排元素的视觉对象。 或者需要能够使用元素据替代,以便派生类可以更改基于元数据的特性,例如默认值。
  • 需要自定义控件的属性接收“属性”窗口编辑等 Visual Studio WPF 设计器支持。 有关详细信息,请参阅控件创作概述

检查这些方案时,还应考虑是否可以通过替代现有依赖属性的元素据而不是通过实现一个全新的属性来实现方案。 元数据替代是否可行取决于方案以及方案与现有 WPF 依赖属性和类中的实现的相似度。 有关替代现有属性上的元素据的详细信息,请参阅依赖属性元素据

依赖属性的定义

  1. 依赖属性的数据类型需要声明为DependencyProperty
  2. 依赖属性声明需要加上public static readonly修饰符
  3. 依赖属性声明需要遵循一定的命名规则。如果使用 Name 注册一个依赖属性,则为依赖属性定义的标识符字段必须命名为 LocationProperty。如果不遵循此命名模式,则设计器可能无法正确地报告属性,而且属性系统样式应用程序的某些方面可能不会以预期的方式工作。
    1. public static readonly DependencyProperty StateProperty;

    依赖属性的注册

    Register()

    | Register(String, Type, Type))
    |
    使用指定的属性名称、属性类型和所有者类型注册依赖属性。
    | | —- | —- | | Register(String, Type, Type, PropertyMetadata))
    |
    使用指定的属性名称、属性类型、所有者类型和属性元数据注册依赖属性。
    | | Register(String, Type, Type, PropertyMetadata, ValidateValueCallback))
    |
    使用指定的属性名称、属性类型、所有者类型、属性元数据和属性的值验证回调来注册依赖属性。 |

示例

  1. public static readonly DependencyProperty IsDirtyProperty = DependencyProperty.Register(
  2. "IsDirty",
  3. typeof(Boolean),
  4. typeof(AquariumObject3)
  5. );
  1. public static readonly DependencyProperty PressImgProperty = DependencyProperty.Register(
  2. "PressImg",
  3. typeof(ImageSource),
  4. typeof(ImageButton)
  5. );

验证回调

应用场景

验证输入的值是否合法,如果属性不合法,可以返回false,则属性赋值失败。如果属性合法,返回true,属性值就会更改,并触发PropertyChanged事件

示例

官方示例

  1. // ValidateValueCallback是验证回调
  2. // IsValidReading是回调绑定的方法
  3. public static readonly DependencyProperty CurrentReadingProperty = DependencyProperty.Register(
  4. "CurrentReading",
  5. typeof(double),
  6. typeof(Gauge),
  7. new FrameworkPropertyMetadata(
  8. Double.NaN,
  9. FrameworkPropertyMetadataOptions.AffectsMeasure,
  10. new PropertyChangedCallback(OnCurrentReadingChanged),
  11. new CoerceValueCallback(CoerceCurrentReading)
  12. ),
  13. new ValidateValueCallback(IsValidReading)
  14. );
  15. public double CurrentReading
  16. {
  17. get { return (double)GetValue(CurrentReadingProperty); }
  18. set { SetValue(CurrentReadingProperty, value); }
  19. }
  20. private static bool IsValidReading(object value)
  21. {
  22. Gauge temp = (Gauge)value;
  23. if(temp.Price < 0)
  24. // 从验证回调返回false后,依赖属性会赋值失败
  25. return false;
  26. else
  27. // 从验证回调返回true后,依赖属性会赋值成功,并触发PropertyChanged事件
  28. return true;
  29. }

个人实践

  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Media;
  4. namespace wpf_control_lib1.Controls
  5. {
  6. /// <summary>
  7. /// 图片按钮
  8. /// </summary>
  9. public class ImageButton:Button
  10. {
  11. /// <summary>
  12. /// 依赖属性包装器 对属性值的验证不写在这里,写在验证回调里
  13. /// </summary>
  14. public ImageSource DefaultImage
  15. {
  16. get { return (ImageSource)GetValue(DefaultImageProperty); }
  17. set { SetValue(DefaultImageProperty, value); }
  18. }
  19. /// <summary>
  20. /// 依赖属性定义
  21. /// </summary>
  22. public static readonly DependencyProperty DefaultImageProperty =
  23. // 依赖属性注册
  24. DependencyProperty.Register("DefaultImage", typeof(ImageSource), typeof(ImageButton),
  25. // 依赖属性元数据 默认值null
  26. new PropertyMetadata(null),
  27. // 验证回调
  28. ValidateValueCallback);
  29. /// <summary>
  30. /// 验证回调,判断是否有效图片
  31. /// </summary>
  32. /// <param name="value"></param>
  33. /// <returns></returns>
  34. private static bool ValidateValueCallback(object value)
  35. {
  36. ImageSource img = value as ImageSource;
  37. if (img != null)
  38. return true;
  39. else
  40. return false;
  41. }
  42. }
  43. }

依赖属性元数据

注册依赖属性时,通过属性系统进行注册会创建一个存储属性特征的元素据对象。 如果将属性注册到的简单签名Register,则其中许多特性都具有默认设置。 通过的Register其他签名,可以指定注册属性时所需的元数据。 为依赖属性使用的最常见元数据是为其使用默认值。该默认值适用于使用此属性的新实例。

参考链接

常用元数据类型

三者的区别和应用场景

官方解析

  1. 如果出于将元数据应用到新依赖属性注册的目的而创建新的元数据实例,则可以选择要使用的元数据类:基 PropertyMetadata 或者某些派生类,例如 FrameworkPropertyMetadata。 通常,应使用 FrameworkPropertyMetadata,尤其是在你的属性与属性系统和 WPF 功能(例如布局和数据绑定)存在任何交互的情况下。 针对更为复杂的方案,还可以从 FrameworkPropertyMetadata 派生以创建自己的元数据报告类,使其成员承载额外的信息。 或者,可以使用 PropertyMetadataUIPropertyMetadata 来传达对实现的功能的支持程度。
  2. 对于现有属性(AddOwnerOverrideMetadata 调用),应始终使用原始注册所用的元数据类型来替代。
  3. 如果要创建在的FrameworkElement派生类上存在的依赖项属性,则可以使用更专用的元数据类FrameworkPropertyMetadata,而不是基类PropertyMetadata。 类的构造函数FrameworkPropertyMetadata有多个签名,你可以在其中组合指定各种元数据特征。 如果只想指定默认值,请使用采用类型Object为的单个参数的签名。 将该对象参数传递为你的属性的特定于类型的默认值 (提供的默认值必须是你作为调用) 中Register的propertyType参数提供的类型。
  4. 对于FrameworkPropertyMetadata,你还可以为属性指定元数据选项标志。 元数据选项标志(FrameworkPropertyMetadataOptions 值的组合)。 这些选项指定依赖项对象的特性,如布局或数据绑定,它们与系统进行交互。 注册后这些标记会转换为属性元数据上的不同属性,并用于将某些条件传送给布局引擎等其他进程。

    参考链接

个人理解

  1. FrameworkPropertyMetadata继承自 UIPropertyMetadataUIPropertyMetadata 继承自PropertyMetadata,子类在继承父类的属性和方法的同时,又添加了自己独有的属性和方法
  2. 基础的DefaultValueCoerceValueCallbackPropertyChangedCallback这三类元数据都具备,而UIPropertyMetadata新增了IsAnimationProhibited属性,可以对动画做启用和禁用。FrameworkPropertyMetadataUIPropertyMetadata的基础上又新增了很多属性和方法,是三类元数据中功能最强大的。但一个对象的属性越多,它占用的空间就越大,所以如果你只需要用到基础功能,那没必要使用FrameworkPropertyMetadata,选择最小的功能几何即可。
  3. 如果你需要用到的属性,在FrameworkPropertyMetadata中已经存在,那优先以FrameworkPropertyMetadata创建元数据
  4. 从三种元数据的非继承属性分析他们的定位

的新增属性:

CoerceValueCallback 获取或设置对此元数据中所指定 CoerceValueCallback 实现的引用。
DefaultValue 获取或设置依赖属性的默认值。
IsSealed 获取一个值,该值确定是否已通过某种方式将元数据应用于属性,从而导致该元数据实例变为不可变状态。
PropertyChangedCallback 获取或设置对此元数据中所指定 PropertyChangedCallback 实现的引用。
  1. using System.Diagnostics;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace wpf_control_lib1.Controls
  6. {
  7. class MyButton:Button
  8. {
  9. public Color UnderlineColor1
  10. {
  11. get { return (Color)GetValue(UnderlineColor1Property); }
  12. set { SetValue(UnderlineColor1Property, value); }
  13. }
  14. // 场景1:只做名称注册及数据类型限制
  15. public static readonly DependencyProperty UnderlineColor1Property =
  16. DependencyProperty.Register("UnderlineColor1", typeof(Color), typeof(MyButton));
  17. public Color UnderlineColor2
  18. {
  19. get { return (Color)GetValue(UnderlineColor2Property); }
  20. set { SetValue(UnderlineColor2Property, value); }
  21. }
  22. // 场景2:需要设置默认值
  23. public static readonly DependencyProperty UnderlineColor2Property =
  24. DependencyProperty.Register("UnderlineColor2", typeof(Color), typeof(MyButton), new PropertyMetadata(Color.FromRgb(255,255,255)));
  25. public Color UnderlineColor3
  26. {
  27. get { return (Color)GetValue(UnderlineColor3Property); }
  28. set { SetValue(UnderlineColor3Property, value); }
  29. }
  30. // 场景3:需要处理PropertyChanged事件或强制回调
  31. public static readonly DependencyProperty UnderlineColor3Property =
  32. DependencyProperty.Register("UnderlineColor3", typeof(Color), typeof(MyButton),
  33. new PropertyMetadata(Color.FromRgb(255, 255, 255), OnUnderlineColorChanged, OnCoerceUnderlineColor));
  34. /// <summary>
  35. /// 依赖属性的值改变时
  36. /// </summary>
  37. /// <param name="sender"></param>
  38. /// <param name="e"></param>
  39. private static void OnUnderlineColorChanged(object sender, DependencyPropertyChangedEventArgs e)
  40. {
  41. // 打印下划线颜色
  42. Trace.WriteLine($"OldValue:{((Color)e.OldValue).ToString()},NewValue:{((Color)e.NewValue).ToString()}");
  43. }
  44. /// <summary>
  45. /// 将value的数据类型强制转换为目标依赖属性的数据类型
  46. /// </summary>
  47. /// <param name="d"></param>
  48. /// <param name="value"></param>
  49. /// <returns></returns>
  50. private static object OnCoerceUnderlineColor(DependencyObject d, object value)
  51. {
  52. // 将输入的十六进制颜色字符串转换为Color
  53. string colorStr = value.ToString();
  54. if (colorStr.StartsWith("#") && colorStr.Length == 7)
  55. {
  56. return ColorConverter.ConvertFromString(colorStr);
  57. }
  58. return Color.FromRgb(255, 255, 255);
  59. }
  60. }
  61. }

的新增属性:

IsAnimationProhibited 获取或设置一个值,声明是否应在应用了包含元数据实例的依赖项对象上禁用动画。
  1. using System.Diagnostics;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace wpf_control_lib1.Controls
  6. {
  7. // 场景:启用或禁用动画
  8. public class ImageButton:Button
  9. {
  10. /// <summary>
  11. /// 是否显示蒙版
  12. /// </summary>
  13. public Visibility MaskVisibility
  14. {
  15. get { return (Visibility)GetValue(MaskVisibilityProperty); }
  16. set { SetValue(MaskVisibilityProperty, value); }
  17. }
  18. public static readonly DependencyProperty MaskVisibilityProperty =
  19. DependencyProperty.Register("MaskVisibility", typeof(Visibility), typeof(ImageButton),
  20. // 依赖属性元数据 最后一个参数为isAnimationProhibited 获取或设置一个值,声明是否应在应用了包含元数据实例的依赖项对象上禁用动画。
  21. // 场景:启用或禁用动画
  22. new UIPropertyMetadata(Visibility.Collapsed, OnUnderlineColorChanged, OnCoerceUnderlineColor, true));
  23. // 依赖属性的值改变时
  24. private static void OnUnderlineColorChanged(object sender, DependencyPropertyChangedEventArgs e)
  25. {
  26. }
  27. // 将value的数据类型强制转换为目标依赖属性的数据类型
  28. private static object OnCoerceUnderlineColor(DependencyObject d, object value)
  29. {
  30. return new Object();
  31. }
  32. }
  33. }

的新增属性

FrameworkPropertyMetadataOptions 元数据选项标志(FrameworkPropertyMetadataOptions 值的组合)。 这些选项指定依赖项对象的特性,如布局或数据绑定,它们与系统进行交互。
UpdateSourceTrigger 应用此属性的绑定时使用的 UpdateSourceTrigger,其 UpdateSourceTrigger 设置为 Default
  1. using System.Diagnostics;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace wpf_control_lib1.Controls
  6. {
  7. class ImgButton:Button
  8. {
  9. public Color UnderlineColor2
  10. {
  11. get { return (Color)GetValue(UnderlineColor2Property); }
  12. set { SetValue(UnderlineColor2Property, value); }
  13. }
  14. // 场景1:通过FrameworkPropertyMetadataOptions指定框架级属性行为中与 Windows Presentation Foundation (WPF) 属性系统中的特定依赖属性相关的类型。
  15. // 例如:FrameworkPropertyMetadataOptions.AffectsRender 更改此依赖属性的值会影响呈现或布局组合的某一方面(不是测量或排列过程)。
  16. public static readonly DependencyProperty UnderlineColor2Property =
  17. DependencyProperty.Register("UnderlineColor2", typeof(Color), typeof(MyButton),
  18. new FrameworkPropertyMetadata(Color.FromRgb(255, 255, 255), FrameworkPropertyMetadataOptions.AffectsRender));
  19. public Color UnderlineColor3
  20. {
  21. get { return (Color)GetValue(UnderlineColor3Property); }
  22. set { SetValue(UnderlineColor3Property, value); }
  23. }
  24. // 场景2:需要同时使用所有可设置属性:默认值、FrameworkPropertyMetadataOptions、属性值改变、强制回调、禁用动画、设置数据绑定值更改触发器类型
  25. public static readonly DependencyProperty UnderlineColor3Property =
  26. DependencyProperty.Register("UnderlineColor3", typeof(Color), typeof(MyButton),
  27. new FrameworkPropertyMetadata(Color.FromRgb(255, 255, 255), FrameworkPropertyMetadataOptions.AffectsRender,
  28. OnUnderlineColorChanged, OnCoerceUnderlineColor, true, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
  29. /// <summary>
  30. /// 依赖属性的值改变时
  31. /// </summary>
  32. /// <param name="sender"></param>
  33. /// <param name="e"></param>
  34. private static void OnUnderlineColorChanged(object sender, DependencyPropertyChangedEventArgs e)
  35. {
  36. // 打印下划线颜色
  37. Trace.WriteLine($"OldValue:{((Color)e.OldValue).ToString()},NewValue:{((Color)e.NewValue).ToString()}");
  38. }
  39. /// <summary>
  40. /// 将value的数据类型强制转换为目标依赖属性的数据类型
  41. /// </summary>
  42. /// <param name="d"></param>
  43. /// <param name="value"></param>
  44. /// <returns></returns>
  45. private static object OnCoerceUnderlineColor(DependencyObject d, object value)
  46. {
  47. // 将输入的十六进制颜色字符串转换为Color
  48. string colorStr = value.ToString();
  49. if (colorStr.StartsWith("#") && colorStr.Length == 7)
  50. {
  51. return ColorConverter.ConvertFromString(colorStr);
  52. }
  53. return Color.FromRgb(255, 255, 255);
  54. }
  55. }
  56. }

默认值

应用场景

在用户未赋值或赋值不符合要求时,会使用默认值,如果依赖属性没有设置默认值,则会报错

个人实践

  1. public Color UnderlineColor2
  2. {
  3. get { return (Color)GetValue(UnderlineColor2Property); }
  4. set { SetValue(UnderlineColor2Property, value); }
  5. }
  6. // 场景2:需要设置默认值
  7. public static readonly DependencyProperty UnderlineColor2Property =
  8. DependencyProperty.Register("UnderlineColor2", typeof(Color), typeof(MyButton),
  9. new PropertyMetadata(Color.FromRgb(255,255,255)));

强制回调

应用场景

  1. 限制最大最小值
  2. 做数据类型转换或匹配

    参考链接

个人实践

示例1
  1. public static readonly DependencyProperty ValueProperty =
  2. DependencyProperty.Register(
  3. "Value", typeof(decimal), typeof(NumericUpDown),
  4. new FrameworkPropertyMetadata(MinValue, new PropertyChangedCallback(OnValueChanged),
  5. new CoerceValueCallback(CoerceValue)));
  6. // 通过强制回调限制最大值
  7. private static object CoerceValue(DependencyObject element, object value)
  8. {
  9. decimal newValue = (decimal)value;
  10. newValue = Math.Max(MinValue, Math.Min(MaxValue, newValue));
  11. return newValue;
  12. }
  13. // 属性值更改事件
  14. private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
  15. {
  16. }

示例2
  1. public static readonly DependencyProperty UnderlineColor3Property =
  2. DependencyProperty.Register("UnderlineColor3", typeof(Color), typeof(MyButton),
  3. new PropertyMetadata(Color.FromRgb(255, 255, 255), OnUnderlineColorChanged, OnCoerceUnderlineColor));
  4. /// 依赖属性的值改变时
  5. private static void OnUnderlineColorChanged(object sender, DependencyPropertyChangedEventArgs e)
  6. {
  7. }
  8. /// <summary>
  9. /// 将value的数据类型强制转换为目标依赖属性的数据类型
  10. /// </summary>
  11. /// <param name="d"></param>
  12. /// <param name="value"></param>
  13. /// <returns></returns>
  14. private static object OnCoerceUnderlineColor(DependencyObject d, object value)
  15. {
  16. // 将输入的十六进制颜色字符串转换为Color
  17. string colorStr = value.ToString();
  18. if (colorStr.StartsWith("#") && colorStr.Length == 7)
  19. {
  20. return ColorConverter.ConvertFromString(colorStr);
  21. }
  22. return Color.FromRgb(255, 255, 255);
  23. }

示例3
  1. using System.Diagnostics;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace wpf_control_lib1.Controls
  6. {
  7. public class ImageButton:Button
  8. {
  9. public Visibility MaskVisibility
  10. {
  11. get { return (Visibility)GetValue(MaskVisibilityProperty); }
  12. set { SetValue(MaskVisibilityProperty, value); }
  13. }
  14. public static readonly DependencyProperty MaskVisibilityProperty =
  15. DependencyProperty.Register("MaskVisibility", typeof(Visibility), typeof(ImageButton),
  16. new UIPropertyMetadata(Visibility.Collapsed, OnUnderlineColorChanged, OnCoerceUnderlineColor, true));
  17. /// 依赖属性的值改变时
  18. private static void OnUnderlineColorChanged(object sender, DependencyPropertyChangedEventArgs e)
  19. {
  20. }
  21. /// <summary>
  22. /// 将value的数据类型强制转换为目标依赖属性的数据类型
  23. /// </summary>
  24. /// <param name="d"></param>
  25. /// <param name="value"></param>
  26. /// <returns></returns>
  27. private static object OnCoerceUnderlineColor(DependencyObject d, object value)
  28. {
  29. if (value == null)
  30. return Visibility.Collapsed;
  31. if (string.IsNullOrEmpty(value.ToString()))
  32. return Visibility.Collapsed;
  33. // 将输入的int值转换为Visibility
  34. if (value.ToString().Equals("0"))
  35. return Visibility.Visible;
  36. else if (value.ToString().Equals("1"))
  37. return Visibility.Hidden;
  38. else if (value.ToString().Equals("2"))
  39. return Visibility.Collapsed;
  40. else
  41. return Visibility.Collapsed;
  42. }
  43. }
  44. }

PropertyChanged

应用场景

  1. 数据改变后做一些逻辑处理
  2. 打印新的数据或旧的数据

    个人实践

    1. public static readonly DependencyProperty UnderlineColor3Property =
    2. DependencyProperty.Register("UnderlineColor3", typeof(Color), typeof(MyButton),
    3. new PropertyMetadata(Color.FromRgb(255, 255, 255), OnUnderlineColorChanged));
    4. /// <summary>
    5. /// 依赖属性的值改变时
    6. /// </summary>
    7. /// <param name="sender"></param>
    8. /// <param name="e"></param>
    9. private static void OnUnderlineColorChanged(object sender, DependencyPropertyChangedEventArgs e)
    10. {
    11. // 打印下划线颜色
    12. Trace.WriteLine($"OldValue:{((Color)e.OldValue).ToString()},NewValue:{((Color)e.NewValue).ToString()}");
    13. }

    强制回调、验证回调、PropertyChanged的执行顺序

    参考链接

个人理解

在一般情况下,执行顺序是验证回调、强制回调、属性值改变事件,即ValidateValueCallbackCoerceValueCallbackPropertyChangedCallback,当我们对依赖属性进行赋值时,首先会校验数据是否符合要求,如果在这一步返回false,则赋值失败,抛出异常。如果数据符合要求,我们可以在强制回调里,转换数据类型或限制最大最小值,最后执行PropertyChangedCallback,此时依赖数据的值已经更改了。我们可以做一些善后工作,或者打印工作。

为依赖属性添加包装器

依赖属性必须通过SetValue(DependencyProperty, Object))赋值,通过GetValue(DependencyProperty))取值,为了方便,我们将依赖属性封装为类似于CLR属性的样子,只需要通过包装器就可以简单实现赋值和取值。

  1. public class MyStateControl : ButtonBase
  2. {
  3. // 包装器
  4. public Boolean State
  5. {
  6. get { return (Boolean)GetValue(StateProperty); }
  7. set { SetValue(StateProperty, value); }
  8. }
  9. public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
  10. "State", typeof(Boolean), typeof(MyStateControl),new PropertyMetadata(false));
  11. }

实践

官方示例

  1. <MyStateControl State="true"/>
  1. public class MyStateControl : ButtonBase
  2. {
  3. public MyStateControl() : base() { }
  4. public Boolean State
  5. {
  6. get { return (Boolean)this.GetValue(StateProperty); }
  7. set { this.SetValue(StateProperty, value); }
  8. }
  9. public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
  10. "State", typeof(Boolean), typeof(MyStateControl),new PropertyMetadata(false));
  11. }

个人实践

示例1

  1. // ValidateValueCallback是验证回调
  2. // IsValidReading是回调绑定的方法
  3. public static readonly DependencyProperty CurrentReadingProperty = DependencyProperty.Register(
  4. "CurrentReading",
  5. typeof(double),
  6. typeof(Gauge),
  7. new FrameworkPropertyMetadata(
  8. Double.NaN,
  9. FrameworkPropertyMetadataOptions.AffectsMeasure,
  10. new PropertyChangedCallback(OnCurrentReadingChanged),
  11. new CoerceValueCallback(CoerceCurrentReading)
  12. ),
  13. new ValidateValueCallback(IsValidReading)
  14. );
  15. public double CurrentReading
  16. {
  17. get { return (double)GetValue(CurrentReadingProperty); }
  18. set { SetValue(CurrentReadingProperty, value); }
  19. }
  20. private static bool IsValidReading(object value)
  21. {
  22. Gauge temp = (Gauge)value;
  23. if(temp.Price < 0)
  24. // 从验证回调返回false后,依赖属性会赋值失败
  25. return false;
  26. else
  27. // 从验证回调返回true后,依赖属性会赋值成功,并触发PropertyChanged事件
  28. return true;
  29. }

示例2

  1. using System.Diagnostics;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace wpf_control_lib1.Controls
  6. {
  7. /// <summary>
  8. /// 图片按钮
  9. /// </summary>
  10. public class ImageButton:Button
  11. {
  12. /// <summary>
  13. /// 依赖属性包装器
  14. /// 是否显示蒙版
  15. /// </summary>
  16. public Visibility MaskVisibility
  17. {
  18. get { return (Visibility)GetValue(MaskVisibilityProperty); }
  19. set { SetValue(MaskVisibilityProperty, value); }
  20. }
  21. /// <summary>
  22. /// 依赖属性定义
  23. /// </summary>
  24. public static readonly DependencyProperty MaskVisibilityProperty =
  25. // 依赖属性注册
  26. DependencyProperty.Register("MaskVisibility", typeof(Visibility), typeof(ImageButton),
  27. // 依赖属性元数据 最后一个参数为isAnimationProhibited 获取或设置一个值,声明是否应在应用了包含元数据实例的依赖项对象上禁用动画。
  28. new UIPropertyMetadata(Visibility.Collapsed, OnUnderlineColorChanged, OnCoerceUnderlineColor, true));
  29. /// <summary>
  30. /// 依赖属性的值改变时
  31. /// </summary>
  32. /// <param name="sender"></param>
  33. /// <param name="e"></param>
  34. private static void OnUnderlineColorChanged(object sender, DependencyPropertyChangedEventArgs e)
  35. {
  36. // 打印
  37. Trace.WriteLine($"OldValue:{((Visibility)e.OldValue).ToString()},NewValue:{((Visibility)e.NewValue).ToString()}");
  38. }
  39. /// <summary>
  40. /// 将value的数据类型强制转换为目标依赖属性的数据类型
  41. /// </summary>
  42. /// <param name="d"></param>
  43. /// <param name="value"></param>
  44. /// <returns></returns>
  45. private static object OnCoerceUnderlineColor(DependencyObject d, object value)
  46. {
  47. if (value == null)
  48. return Visibility.Collapsed;
  49. if (string.IsNullOrEmpty(value.ToString()))
  50. return Visibility.Collapsed;
  51. // 将输入的int值转换为Visibility
  52. if (value.ToString().Equals("0"))
  53. return Visibility.Visible;
  54. else if (value.ToString().Equals("1"))
  55. return Visibility.Hidden;
  56. else if (value.ToString().Equals("2"))
  57. return Visibility.Collapsed;
  58. else
  59. return Visibility.Collapsed;
  60. }
  61. }
  62. }

示例3

  1. using System.Diagnostics;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace wpf_control_lib1.Controls
  6. {
  7. class ImgButton:Button
  8. {
  9. public Color UnderlineColor2
  10. {
  11. get { return (Color)GetValue(UnderlineColor2Property); }
  12. set { SetValue(UnderlineColor2Property, value); }
  13. }
  14. // 场景1:通过FrameworkPropertyMetadataOptions指定框架级属性行为中与 Windows Presentation Foundation (WPF) 属性系统中的特定依赖属性相关的类型。
  15. // 例如:FrameworkPropertyMetadataOptions.AffectsRender 更改此依赖属性的值会影响呈现或布局组合的某一方面(不是测量或排列过程)。
  16. public static readonly DependencyProperty UnderlineColor2Property =
  17. DependencyProperty.Register("UnderlineColor2", typeof(Color), typeof(MyButton),
  18. new FrameworkPropertyMetadata(Color.FromRgb(255, 255, 255), FrameworkPropertyMetadataOptions.AffectsRender));
  19. public Color UnderlineColor3
  20. {
  21. get { return (Color)GetValue(UnderlineColor3Property); }
  22. set { SetValue(UnderlineColor3Property, value); }
  23. }
  24. // 场景2:需要同时使用所有可设置属性:默认值、FrameworkPropertyMetadataOptions、属性值改变、强制回调、禁用动画、设置数据绑定值更改触发器类型
  25. public static readonly DependencyProperty UnderlineColor3Property =
  26. DependencyProperty.Register("UnderlineColor3", typeof(Color), typeof(MyButton),
  27. new FrameworkPropertyMetadata(Color.FromRgb(255, 255, 255), FrameworkPropertyMetadataOptions.AffectsRender,
  28. OnUnderlineColorChanged, OnCoerceUnderlineColor, true, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));
  29. /// <summary>
  30. /// 依赖属性的值改变时
  31. /// </summary>
  32. /// <param name="sender"></param>
  33. /// <param name="e"></param>
  34. private static void OnUnderlineColorChanged(object sender, DependencyPropertyChangedEventArgs e)
  35. {
  36. // 打印下划线颜色
  37. Trace.WriteLine($"OldValue:{((Color)e.OldValue).ToString()},NewValue:{((Color)e.NewValue).ToString()}");
  38. }
  39. /// <summary>
  40. /// 将value的数据类型强制转换为目标依赖属性的数据类型
  41. /// </summary>
  42. /// <param name="d"></param>
  43. /// <param name="value"></param>
  44. /// <returns></returns>
  45. private static object OnCoerceUnderlineColor(DependencyObject d, object value)
  46. {
  47. // 将输入的十六进制颜色字符串转换为Color
  48. string colorStr = value.ToString();
  49. if (colorStr.StartsWith("#") && colorStr.Length == 7)
  50. {
  51. return ColorConverter.ConvertFromString(colorStr);
  52. }
  53. return Color.FromRgb(255, 255, 255);
  54. }
  55. }
  56. }

示例4

  1. using System.Diagnostics;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace wpf_control_lib1.Controls
  6. {
  7. class MyButton:Button
  8. {
  9. public Color UnderlineColor1
  10. {
  11. get { return (Color)GetValue(UnderlineColor1Property); }
  12. set { SetValue(UnderlineColor1Property, value); }
  13. }
  14. // 场景1:只做名称注册及数据类型限制
  15. public static readonly DependencyProperty UnderlineColor1Property =
  16. DependencyProperty.Register("UnderlineColor1", typeof(Color), typeof(MyButton));
  17. public Color UnderlineColor2
  18. {
  19. get { return (Color)GetValue(UnderlineColor2Property); }
  20. set { SetValue(UnderlineColor2Property, value); }
  21. }
  22. // 场景2:需要设置默认值
  23. public static readonly DependencyProperty UnderlineColor2Property =
  24. DependencyProperty.Register("UnderlineColor2", typeof(Color), typeof(MyButton), new PropertyMetadata(Color.FromRgb(255,255,255)));
  25. public Color UnderlineColor3
  26. {
  27. get { return (Color)GetValue(UnderlineColor3Property); }
  28. set { SetValue(UnderlineColor3Property, value); }
  29. }
  30. // 场景3:需要处理PropertyChanged事件或强制回调
  31. public static readonly DependencyProperty UnderlineColor3Property =
  32. DependencyProperty.Register("UnderlineColor3", typeof(Color), typeof(MyButton),
  33. new PropertyMetadata(Color.FromRgb(255, 255, 255), OnUnderlineColorChanged, OnCoerceUnderlineColor));
  34. /// <summary>
  35. /// 依赖属性的值改变时
  36. /// </summary>
  37. /// <param name="sender"></param>
  38. /// <param name="e"></param>
  39. private static void OnUnderlineColorChanged(object sender, DependencyPropertyChangedEventArgs e)
  40. {
  41. // 打印下划线颜色
  42. Trace.WriteLine($"OldValue:{((Color)e.OldValue).ToString()},NewValue:{((Color)e.NewValue).ToString()}");
  43. }
  44. /// <summary>
  45. /// 将value的数据类型强制转换为目标依赖属性的数据类型
  46. /// </summary>
  47. /// <param name="d"></param>
  48. /// <param name="value"></param>
  49. /// <returns></returns>
  50. private static object OnCoerceUnderlineColor(DependencyObject d, object value)
  51. {
  52. // 将输入的十六进制颜色字符串转换为Color
  53. string colorStr = value.ToString();
  54. if (colorStr.StartsWith("#") && colorStr.Length == 7)
  55. {
  56. return ColorConverter.ConvertFromString(colorStr);
  57. }
  58. return Color.FromRgb(255, 255, 255);
  59. }
  60. }
  61. }