加入Binding以收听没有FrameworkElement的PropertyChanged事件 [英] Co-opting Binding to listen to PropertyChanged events without a FrameworkElement

查看:85
本文介绍了加入Binding以收听没有FrameworkElement的PropertyChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些实现 INotifyPropertyChanged 的嵌套视图模型。我想绑定一个事件侦听器到嵌套的属性路径(例如Parent.Child.Name),很像 FrameworkElement 依赖属性可以绑定到任意的嵌套属性。



但是,我只是想要一个类似于 PropertyChanged 事件侦听器 - 我实际上没有任何UI元素我想绑定。有没有办法使用现有的框架来设置这样一个事件源?理想情况下,我不需要修改我的视图模型类(因为这不是Silverlight中常规数据绑定所必需的)。

解决方案

p>您当然可以使用绑定/依赖属性基础结构来监听嵌套属性的更改。下面的代码是WPF,但我相信你可以在Silverlight中做类似的事情:

  public partial class MainWindow:Window 
{
public MainWindow()
{
InitializeComponent();

this.DataContext = new Parent {Child = new Child {Name =Bob}};
this.SetBinding(ChildNameProperty,new Binding(Child.Name));
}

public string ChildName
{
get {return(string)GetValue(ChildNameProperty); }
set {SetValue(ChildNameProperty,value); }
}

//使用DependencyProperty作为ChildName的后备存储。这使得动画,样式,绑定等...
public static readonly DependencyProperty ChildNameProperty =
DependencyProperty.Register(ChildName,typeof(string),typeof(MainWindow),new UIPropertyMetadata(ChildNameChanged)) ;

static void ChildNameChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
MessageBox.Show(Child name is now+ e.NewValue);
}
}

所以我已经定义了我自己的 DependencyProperty ,不是任何UI本身的一部分(只是 MainWindow 类),并直接绑定Child.Name。当 Child.Name 更改时,我可以收到通知。



这是否适合您? p>

I have some nested view models that implement INotifyPropertyChanged. I'd like to bind an event listener to a nested property path (e.g. "Parent.Child.Name"), much like FrameworkElement dependency properties can be bound to arbitrary nested properties.

However, I just want something like a PropertyChanged event listener -- I don't actually have any UI element I'd like to bind. Is there any way to use the existing framework to set up such an event source? Ideally, I shouldn't need to modify my view model classes (as this is not required for regular data binding in Silverlight).

解决方案

You can certainly co-opt the binding/dependency-property infrastructure to listen for changes to a nested property. The code below is WPF but I believe you can do something similar in Silverlight:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = new Parent { Child = new Child { Name = "Bob" } };
        this.SetBinding(ChildNameProperty, new Binding("Child.Name"));
    }

    public string ChildName
    {
        get { return (string)GetValue(ChildNameProperty); }
        set { SetValue(ChildNameProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ChildName.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ChildNameProperty =
        DependencyProperty.Register("ChildName", typeof(string), typeof(MainWindow), new UIPropertyMetadata(ChildNameChanged));

    static void ChildNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MessageBox.Show("Child name is now " + e.NewValue);
    }
}

So I've defined my own DependencyProperty, not part of any UI per se (just the MainWindow class), and bound "Child.Name" to it directly. I'm then able to be notified when Child.Name changes.

Will that work for you?

这篇关于加入Binding以收听没有FrameworkElement的PropertyChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆