WinRT程序中Control没有DataContextChanged事件,导致在使用过程中带来不少不便,如何获取DataContext变化事件,本文给了一个简单的解决方案:

首先注册一个带回调函数的依赖属性:

    public static readonly DependencyProperty MyDataContextProperty =
        DependencyProperty.Register("MyDataContext",
                                    typeof(Object),
                                    typeof(MainPage),
                                    new PropertyMetadata(MyDataContextChanged));    

然后,将依赖属性和我们要监控的DataContext绑定起来,

    SetBinding(MyDataContextProperty, new Binding());

这样,当依赖属性MyDataCntextProperty变化时,便会执行回调函数MyDataContextChanged,通过回调函数的参数即可获取DataContext的变化。

    private static void MyDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("DataContext Changed. Old value:{0}, New value {1}", e.OldValue, e.NewValue);
    }

 

相关文章:

  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
  • 2021-05-22
  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-13
  • 2022-03-05
  • 2021-12-24
  • 2021-06-29
  • 2021-08-03
  • 2022-02-25
  • 2022-01-03
相关资源
相似解决方案