如何在WinRT中监听Dependency属性更改?

时间:2015-04-14 09:13:35

标签: c# winrt-xaml

在WPF中,我们可以使用以下代码监听Dependency属性更改。

 FlowDirectionProperty.OverrideMetadata(typeof(DataGrid),
 new FrameworkPropertyMetadata(OnFlowDirectionChanged));

但是如何在WinRT中监听属性的变化?

1 个答案:

答案 0 :(得分:2)

您需要编写一个处理程序来通知依赖项属性的更改。

使用以下语法

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register(
        "Value",
        typeof(object),
        typeof(DependencyPropertyWatcher),
        new PropertyMetadata(null, OnPropertyChanged));

public event EventHandler PropertyChanged;

 public DependencyPropertyWatcher(DependencyObject target, string propertyPath)
 {
        this.Target = target;
        BindingOperations.SetBinding(
        this,
        ValueProperty,
        new Binding() { Source = target, Path = new PropertyPath(propertyPath), Mode = BindingMode.OneWay });
}

有关其他详细信息,请浏览此链接 http://blogs.msdn.com/b/flaviencharlon/archive/2012/12/07/getting-change-notifications-from-any-dependency-property-in-windows-store-apps.aspx