当前位置:网站首页>【wpf】依赖属性三个回调详解
【wpf】依赖属性三个回调详解
2022-08-06 03:29:00 【code bean】
三种回调:
1 PropertyChangedCallback 值回调
2 CoerceValueCallback 强制回调
3 ValidateValueCallback 验证回调
依赖属性包含三个回调的写法
public int YL
{
get { return (int)GetValue(YLProperty); }
set { SetValue(YLProperty, value); }
}
// Using a DependencyProperty as the backing store for YL. This enables animation, styling, binding, etc...
public static readonly DependencyProperty YLProperty =
DependencyProperty.Register("YL", typeof(int), typeof(TestMode),
new PropertyMetadata(0,
new PropertyChangedCallback(OnPropertyChanged), // 第一个值回调位置
new CoerceValueCallback(OnCoerceValueCallback) // 第三个强制回调位置
)
,new ValidateValueCallback(OnValidateValueCallBack)//第二个值验证回调位置
);
static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MessageBox.Show($"第3个 值回调 old:{e.OldValue} new:{e.NewValue}");
}
static object OnCoerceValueCallback(DependencyObject d, object obj)
{
MessageBox.Show($"第2个 强制回调({obj})");
if (int.Parse(obj.ToString()) > 1000)
return 2000;
return obj;
}
static bool OnValidateValueCallBack(object obj)
{
var r = int.Parse(obj.ToString());
MessageBox.Show($"第1个 值验证回调 val:{r}");
if (r > 100 && r < 200)
return false;
// 绑定的时候可以把异常捕获到进行提示
return true;
}
值回调,PropertyChangedCallback
当属性的值发生改变的时候,才会触发该回调函数。通过参数e能拿到e.OldValue和 e.NewValue
此时,我们还可以为PropertyChangedCallback 上一道“保险”:即使属性值变了,
但是,这个时可能太离谱,所以不要促发“值回调”,因为没有意义。
此时的这道保险就是,ValidateValueCallback 验证回调。
(值回调和验证回调同属于PropertyMetadata构造函数的参数。)
它需要传入一个返回bool类型的函数,如果函数返回false,就表示不要惊动值回调了。
ValidateValueCallback的出发条件就是属性被赋值。
最后看看 CoerceValueCallback 强制回调
它属于DependencyProperty.Register中的参数。
在介绍这个回调之前,要先看看这三个回调的顺序,当属性被赋值时,最先被出发的是验证回调,
如果验证回调返回false,就没其他回调什么事了。如果返回true,第二个被触发的就是强制回调,
它的目的时返回一个值,并且这个值会直接赋值给属性(此时的属性赋值不会再次出发自己,如果返回的这个值和之前的不一样,此时还会再次出发验证回调!且此时触发也在在值回调之前)
最后才是值回调,如果此时验证回调返回ture,且强制回调的值和之前的值不一样,那么此时值回调才会被触发。
可以写个程序验证一下:

边栏推荐
- xctf攻防世界 Web高手进阶区 easytornado
- django报错 ModuleNotFoundError: No module named 'mysqlclient'
- 什么浏览器广告少?多御安全浏览器轻体验
- firewall and ufw notes
- Entering the pit of machine learning: 2. Supervised learning
- sql server, two tables, the same field, the same amount of data (50 million), execute the same query, one table executes an index scan, scanning 50 million, which takes 2 minutes, the other table exec
- cocos小游戏实战-完结
- 3D激光SLAM:LIO-SAM整体介绍与安装编译
- Lvm根分区扩容
- C 学生管理系统 删除指定学生节点(一般情况)
猜你喜欢

pytest之assert断言的使用

经典sql例子

离散数学期末习题
【无标题】菜市场

Failed to save image in R language ggplot2 loop

通行宝通过注册:年营收5.9亿拟募资5.6亿 腾讯云与上汽是股东

What is an Egg. The js, it have what features, installation of an Egg framework, definition of routing, what's Controller is the Controller, CORS configuration, the json configuration, a get request,

学习MySQL的第三天:函数(基础篇)

2022 Alibaba Cloud server configuration selection strategy

xctf attack and defense world web master advanced area command_execution
随机推荐
What are the highlights of the 2022 Jingdong New Department Store Qixi Festival Promotion Season?
Several common misunderstandings of enterprises and webmasters renting servers in 2022
面试官:同学,冒泡太简单了,要不手写一个【快速排序】吧...
入坑机器学习:三,非监督学习
Crypto Bear Market Offers M&A Opportunity Ambitious or Savior?The only truth is that interests come first
KU115 PCIE bus data preprocessing board (multi-LVDS interface)
NetCore - custom exception handling
[Deep Learning 21 Days Learning Challenge] Memo: Model Reuse - Model Saving and Loading
Wang Qi, Secretary of the Party Committee and President of Shanghai Pudong Development Bank Changsha Branch, and Jiang Junwen, President of Huarong Xiangjiang Bank, visited Kylin Principal for investi
[Untitled] Directory Test
Netcore——Result
我所理解liunx下的原子操作
greenDAO of Android database framework
sql server, two tables, the same field, the same amount of data (50 million), execute the same query, one table executes an index scan, scanning 50 million, which takes 2 minutes, the other table exec
xctf attack and defense world web master advanced area command_execution
Sorting out the knowledge system of fully homomorphic encryption
AC8015 Notes
译文推荐|深入解析 BookKeeper 协议模型与验证
LeetCode:623. 在二叉树中增加一行【DFS or BFS】
2022年阿里云服务器配置选取攻略