#150 – An Example of Using PropertyChanged and CoerceValue Callbacks(PropertyChanged和CoerceValue例子)
来源:互联网 发布:三只眼漫画软件 编辑:程序博客网 时间:2024/05/17 08:41
在使用依赖属性的时候,有的时候可能会遇到需要强制限定多个依赖属性之间的关系的情况,这个时候一般会用CoerceValueCallback 和PropertyChangedCallbacks 来实现。
举个例子,假定我们有一个Person 类,有BirthYear(出生)、DeathYear(去世) 和MarriageYear(结婚) 三个属性。我们确定结婚时间必须在出生和去世之间,出生时间必须早于另外两个,而去世时间不得早于出生和结婚。
BirthYear 属性
public int BirthYear { get { return (int)GetValue(BirthYearProperty); } set { SetValue(BirthYearProperty, value); } } public static readonly DependencyProperty BirthYearProperty = DependencyProperty.Register("BirthYear", typeof(int), typeof(Person), new PropertyMetadata( 1900, // Default new PropertyChangedCallback(OnBirthYearChanged))); public static void OnBirthYearChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Person p = (Person)d; p.CoerceValue(DeathYearProperty); p.CoerceValue(MarriageYearProperty); }
DeathYear 属性
public int DeathYear { get { return (int)GetValue(DeathYearProperty); } set { SetValue(DeathYearProperty, value); } } // Using a DependencyProperty as the backing store for DeathYear. This enables animation, styling, binding, etc... public static readonly DependencyProperty DeathYearProperty = DependencyProperty.Register("DeathYear", typeof(int), typeof(Person), new PropertyMetadata( 1900, // Default new PropertyChangedCallback(OnDeathYearChanged), new CoerceValueCallback(CoerceDeathYear))); public static void OnDeathYearChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Person p = (Person)d; p.CoerceValue(MarriageYearProperty); } public static object CoerceDeathYear(DependencyObject d, object value) { Person p = (Person)d; int deathYear = (int)value; if (deathYear < p.BirthYear) deathYear = p.BirthYear; return deathYear; }
MarriageYear 属性
public int MarriageYear{ get { return (int)GetValue(MarriageYearProperty); } set { SetValue(MarriageYearProperty, value); }} // Using a DependencyProperty as the backing store for DeathYear. This enables animation, styling, binding, etc...public static readonly DependencyProperty MarriageYearProperty = DependencyProperty.Register("MarriageYear", typeof(int), typeof(Person), new PropertyMetadata( 1900, // Default null, new CoerceValueCallback(CoerceMarriageYear))); public static object CoerceMarriageYear(DependencyObject d, object value){ Person p = (Person)d; int marriageYear = (int)value; if (marriageYear < p.BirthYear) marriageYear = p.BirthYear; if (marriageYear > p.DeathYear) marriageYear = p.DeathYear; return marriageYear;}
0 0
- #150 – An Example of Using PropertyChanged and CoerceValue Callbacks(PropertyChanged和CoerceValue例子)
- PropertyChanged 事件-数据绑定
- an example of using automake.
- An example of using Thrift
- WPF中ListBox与PropertyChanged
- An easy example of using AJAX
- An example of using STL multimap
- An example of Integrating Spark and Cassandra
- An example of using libexif to set JPEG Orientation Tag
- An example of using libexif to set JPEG Orientation Tag
- an example of drawing beast tree using ggtree
- 两个用C实现遗传算法的程序 an example of a very simple genetic algorithm in C and a GA implementation using binary and real coded variables
- An example in sklearn: Faces recognition example using eigenfaces and SVMs
- An example of the <sql> and <include> tags
- an example of repeater
- Example of Using Google Maps Service and GPS in Android
- Dead simple example of using Multiprocessing Queue, Pool and Locking
- a complete example of using One to Many and One to One associations using MyBatis annotations
- 687D Dividing Kingdom II(二分图,并茶几)
- 调用webservice服务(二) 学习笔记/cxf拦截器
- 关于App启动加载广告页面思路
- perl 异步请求和JS对比
- 让UITableView的section header view不悬停的方法
- #150 – An Example of Using PropertyChanged and CoerceValue Callbacks(PropertyChanged和CoerceValue例子)
- The purpose of learning Japanese
- iOS --TableView与NavigationBar中间多出空白间隔
- 测试Css3跟html5支持度的网址
- 数据结构与算法简记:快速排序
- 设计模式C++实现(5)——原型模式、模板方法模式
- jquery实现上一页 下一页
- CountDownLatch
- 160701 iconfont的使用