Silverlight:如何在继承的DependencyProperty中接收更改的通知 [英] Silverlight: How to receive notification of a change in an inherited DependencyProperty

查看:61
本文介绍了Silverlight:如何在继承的DependencyProperty中接收更改的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控件(该控件继承自您)。
每当更改 FontSize Style 属性时,我都希望收到通知。在WPF中,我可以通过调用 DependencyProperty.OverrideMetadata()来实现。当然,类似的有用的东西在Silverlight中没有位置。那么,一个人怎么会收到这类通知呢?

I have a control which inherits from (you guessed it) Control. I want to receive a notification whenever the FontSize or Style properties are changed. In WPF, I would do that by calling DependencyProperty.OverrideMetadata(). Of course, useful things like that have no place in Silverlight. So, how might one receive those kinds of notifications?

推荐答案

我认为这是一种更好的方法。

I think here is a better way. Still need to see the pros and Cons.

 /// Listen for change of the dependency property
    public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
    {

        //Bind to a depedency property
        Binding b = new Binding(propertyName) { Source = element };
        var prop = System.Windows.DependencyProperty.RegisterAttached(
            "ListenAttached"+propertyName,
            typeof(object),
            typeof(UserControl),
            new System.Windows.PropertyMetadata(callback));

        element.SetBinding(prop, b);
    }

现在,您可以调用RegisterForNotification来注册属性更改通知

And now, you can call RegisterForNotification to register for a change notification of a property of an element, like .

RegisterForNotification("Text", this.txtMain,(d,e)=>MessageBox.Show("Text changed"));
RegisterForNotification("Value", this.sliderMain, (d, e) => MessageBox.Show("Value changed"));

在同一 http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html

使用Silverlight 4.0 Beta。

Using Silverlight 4.0 beta.

这篇关于Silverlight:如何在继承的DependencyProperty中接收更改的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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