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();
    }
}
相关推荐
whn197711 小时前
oracle的节点2无法启动asm实例 提示PMON terminating the instance due to error 481
数据库·oracle·wpf
完美火龙篇 四月的友11 小时前
WPF 笔迹延迟优化从硬件到软件的全链路分析
wpf
dalong1011 小时前
WPF:3D立方体
3d·wpf
Xin_ye100862 天前
第三章:内存泄漏的常见“案发现场”
c#·wpf
心平气和量大福大2 天前
C#-WPF-UserControl-生命周期(加载 退出)
开发语言·c#·wpf
czhc11400756632 天前
7.23:Claude code:注册->封表
wpf
心平气和量大福大3 天前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
listening7773 天前
HarmonyOS 6.1 全场景协同实战:从“单机”到“超级终端”的分布式重构
wpf
心平气和量大福大4 天前
C#-WPF-布局-Grid
c#·wpf·visual studio
心平气和量大福大5 天前
C#-WPF-Window主窗体
开发语言·c#·wpf