当前位置:网站首页>[wpf] Detailed explanation of three callbacks for dependency properties
[wpf] Detailed explanation of three callbacks for dependency properties
2022-08-06 03:32:00 【代码bean】
Three callbacks:
1 PropertyChangedCallback value callback
2 CoerceValueCallback mandatory callback
3 ValidateValueCallback validation callback
The dependency property contains three callbacks
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), // first value callback positionnew CoerceValueCallback(OnCoerceValueCallback) // third forced callback position),new ValidateValueCallback(OnValidateValueCallBack)//The second value validation callback position);static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){MessageBox.Show($"The 3rd value callback old:{e.OldValue} new:{e.NewValue}");}static object OnCoerceValueCallback(DependencyObject d, object obj){MessageBox.Show($"The second mandatory callback ({obj})");if (int.Parse(obj.ToString()) > 1000)return 2000;return obj;}static bool OnValidateValueCallBack(object obj){var r = int.Parse(obj.ToString());MessageBox.Show($"The first value validation callback val:{r}");if (r > 100 && r < 200)return false;// When binding, the exception can be caught for promptingreturn true;}Value callback, PropertyChangedCallback
The callback function is triggered when the value of the property changes.You can get e.OldValue and e.NewValue through the parameter e
At this point, we can also provide an "insurance" for PropertyChangedCallback: even if the property value changes,
However, this time may be outrageous, so don't trigger a "value callback" because it doesn't make sense.
The insurance at this time is, ValidateValueCallback validation callback.
(The value callback and validation callback are both parameters of the PropertyMetadata constructor.)
It needs to pass in a function that returns a bool type. If the function returns false, it means that the value callback should not be alerted.
The starting condition of ValidateValueCallback is that the property is assigned a value.
Last look at CoerceValueCallback force callback
It is a parameter in DependencyProperty.Register.
Before introducing this callback, let's take a look at the order of these three callbacks. When the property is assigned, the first is the verification callback,
If the validation callback returns false, nothing else happens to the other callbacks.If it returns true, the second trigger is Force callback,
It returns a value for its purpose, and this value will be directly assigned to the attribute (at this time, the attribute assignment will not start itself again. If the returned value is different from the previous one, the verification callback will be started again at this time.!And the trigger is also before the value callback)
The last is the value callback. If the verification callback returns true at this time, and the value of the mandatory callback is different from the previous value, then the value callback will be triggered at this time.
You can write a program to verify:

边栏推荐
猜你喜欢

Introduction to Elliptic Curves (4): Elliptic Curve Security, Compared with RSA

leetcode 17. 电话号码的字母组合

Literature Reading---Analysis of Genome Haplotypes and Genome Stability and Creeping of Common Bermudagrass Yangjiang

pytest之assert断言的使用

加密熊市为企业并购提供机遇 野心勃勃or救世主?唯一真理便是利益至上

造自己的芯,让谷歌买单!谷歌再度开源 180nm 工艺的芯片

Django reports an error ModuleNotFoundError: No module named 'mysqlclient'

What are solid pneumatic tires?

物联网协议概述

leetcode 17. Letter Combinations of Phone Numbers
随机推荐
I understand atomic operations under linux
Deep Learning Course2 Week 2 Hyperparameter tuning, Batch Normalization, Programming Frameworks exercises
firewall 和ufw 笔记
Use BeanUtils with caution, the performance really stretches!
Advantages and disadvantages of solid tires
django报错 ModuleNotFoundError: No module named 'mysqlclient'
6. -- -- -- -- -- test automation of software testing unittest framework
Web3.exceptions.ExtraDataLengthError
数字化智能工厂解决方案47页
What is an inner class?
C Student management system Delete the specified student node (special case)
【Translation】Serverless Architecture: Pros and Cons
邓晔研究组在微生物功能基因的数据库开发和环境定量方面取得新进展
全同态加密知识体系整理
FluentValidation
Fragment的四种跳转方式
智能合约安全——随机数
The delivery language set in the Google Ads background, if English is set, does it mean that the user is using English?
如果一个国家市场效果特别好,我们如何加大这个国家的投放比例。
AC8015笔记