我正在实现UserControl
,我想检测DataContext是否从代码中更改。FrameworkElement.DataContext.Get
不是虚拟的,所以我无法覆盖它。我可以将其隐藏起来,new
但是我相信有更好的方法可以做到这一点。在WPF中,有一些类似DataContextChanged事件的事件。我们可以使用Windows Phone做类似的事情吗?
在您的用户控件构造函数中添加以下内容:
this.SetBinding(BoundDataContextProperty, new Binding());
然后添加这些:
public static readonly DependencyProperty BoundDataContextProperty = DependencyProperty.Register(
"BoundDataContext",
typeof(object),
typeof(MyUserControl),
new PropertyMetadata(null, OnBoundDataContextChanged));
private static void OnBoundDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// e.NewValue is your new DataContext
// d is your UserControl
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句