#117 – Registering a Dependency Property(注册一个依赖属性)
来源:互联网 发布:海康ntp服务器地址端口 编辑:程序博客网 时间:2024/05/12 22:47
原文地址:https://wpf.2000things.com/2010/11/06/117-registering-a-dependency-property/
为了使用依赖属性,要在使用的类中创建一个DependencyProperty 类的静态实例。之所以创建静态的实例是因为在DependencyProperty 类中仅仅只是描述这个依赖属性,而并没有真正的存储这个依赖属性的值。
你可以通过下面的方法声明一个静态属性:
public static readonly DependencyProperty AgeProperty;
我们通常使用静态方法 DependencyProperty.Register 构造并注册一个依赖属性。
static Person() { PropertyMetadata ageMetadata = new PropertyMetadata( 18, // Default value new PropertyChangedCallback(OnAgeChanged), new CoerceValueCallback(OnAgeCoerceValue)); // Register the property AgeProperty = DependencyProperty.Register( "Age", // Property's name typeof(int), // Property's type typeof(Person), // Defining class' type ageMetadata, // Defines default value & callbacks (optional) new ValidateValueCallback(OnAgeValidateValue)); // validation (optional) }
前3个参数是注册函数Register 必须的。
你同样还可以通过该函数设置:
-属性的默认值
-属性值改变时触发的函数
-属性值验证回调函数(Coercion and validation callbacks)
在定义依赖属性的时候,应该将它设置为 public static readonly
0 0
- #117 – Registering a Dependency Property(注册一个依赖属性)
- #122 – Validating a Dependency Property(验证依赖属性)
- #159 – Creating a Read-Only Dependency Property(创建一个只读的依赖属性)
- #167 – Implementing a Dependency Property That Is A Collection(实现一个Collection类型的依赖属性)
- Dependency Property 依赖属性
- 依赖属性Dependency Property
- #120 – Clearing a Dependency Property Value(清除依赖属性的值)
- #123 – Coercing a Dependency Property(强制转换依赖属性的值)
- WPF中的依赖属性(Dependency Property)
- Silverlight:Dependency Property(依赖属性)
- #128 – Using Code Snippets to Implement a Dependency Property(用代码段实现依赖属性)
- #158 – When to Create a Custom Dependency Property(哪些情况要创建自定义依赖属性)
- #600 – 注册一个路由事件(Registering a Routed Event)
- #154 – Reusing an Existing Dependency Property in Your Class(重用依赖属性)
- Silverlight:Dependency Property(依赖属性)学习笔记
- Silverlight:Dependency Property(依赖属性)学习笔记
- #116 – Dependency Property Values Are Stored in the DependencyObject(依赖属性存储在DependencyObject中)
- 依赖属性 dependency property WPF,为什么要用DDP?
- Android学习笔记----解决“com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536”问题
- 可空值类型
- 使用SVG开发音乐播放器(二)
- 前端面试题
- jsessionid的简单说明
- #117 – Registering a Dependency Property(注册一个依赖属性)
- MBX_Main()
- DRP之JavaScript基础整理
- checkbox获取值
- Windows7安装和使用Inforbright
- activi参考网址
- WebRTC源代码探索之旅——多线程篇-1
- 最小路径覆盖_zoj1525_最小路径覆盖+水
- MD5加密工具类