WPF 依赖属性改变触发响应事件

WPF 依赖属性改变触发响应事件

  在书写依赖属性时,如果后台数据发生了变化,我们会发现依赖属性如果不为他设置对应的响应事件,他是不会做任何操作的
解决方案:

当我们使用 DependencyProperty.Register 函数注册依赖属性时,可以对其参数四调用 PropertyMetadata(object defaultValue, PropertyChangedCallback propertyChangedCallback) 构造函数,设置 PropertyChangedCallback 回调函数来获取修改后的数据

   PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) 值回调

     当属性的值发生改变的时候,会触发该回调函数

       参数 d 可以拿到其对象

       参数e能拿到 其 e.OldValue 和 e.NewValue

csharp 复制代码
public string VideoPath
{
    get { return (string)GetValue(VideoPathProperty); }
    set { SetValue(VideoPathProperty, value);}
}
public static readonly DependencyProperty VideoPathProperty =
    DependencyProperty.Register("VideoPath", typeof(string), typeof(TestUsercontrol), new PropertyMetadata(string.Empty, PropertyChangedCallback));

static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var test = d as TestUsercontrol;
    if (null != test)
    {
    	var value = e.NewValue;
		test.Play();
    }
}
相关推荐
迪飞特科技3 小时前
分布式事务下 Spring 声明式事务失效的 3 种修复方案
分布式·spring·wpf
2601_962174173 天前
Redis 简介
数据库·redis·wpf
SamChan904 天前
Python+PyMuPDF提取PDF表格并翻译:表格结构检测与双语重建实战
windows·python·ai·pdf·wpf
SamChan904 天前
Python+OpenCV检测PDF翻译后排版偏移:像素级格式一致性验证
python·opencv·ai·pdf·wpf
FuckPatience4 天前
WPF 拿到控件的最初样式,修改获得焦点样式
wpf
主宰者4 天前
【WPF】MainWindow前添加加载窗口,发现加载过程中有白色无内容窗口闪烁一下的解决方案
wpf
Vae_Mars5 天前
WPF中的ControlTemplate(控件模板)
wpf
SamChan905 天前
Python+ReportLab自动生成PDF翻译质量审计报告:从数据到可视化的完整方案
开发语言·python·ai·pdf·wpf
circuitsosk5 天前
多智能体协同在能源数据分析中的实践:分配-执行-校验闭环架构设计
python·数据分析·wpf·能源·并行计算·多智能体系统·agent协作
weixin199701080166 天前
《大促护航:电商API限流与降级,双11峰值500万调用的架构复盘》(附Python源码)
python·架构·wpf