0
点赞
收藏
分享

微信扫一扫

现代前端构建工具对比:Vue CLI、Webpack 和 Vite

东林梁 2024-09-25 阅读 14
wpf

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

  在书写依赖属性时,如果后台数据发生了变化,我们会发现依赖属性如果不为他设置对应的响应事件,他是不会做任何操作的
解决方案:
当我们使用 DependencyProperty.Register 函数注册依赖属性时,可以对其参数四调用 PropertyMetadata(object defaultValue, PropertyChangedCallback propertyChangedCallback) 构造函数,设置 PropertyChangedCallback 回调函数来获取修改后的数据
   PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) 值回调
     当属性的值发生改变的时候,会触发该回调函数
       参数 d 可以拿到其对象
       参数e能拿到 其 e.OldValue 和 e.NewValue

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();
    }
}
举报

相关推荐

0 条评论