微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight:如何接收继承的DependencyProperty中的更改通知

我有一个控制继承自(你猜到它)控制.
每当更改FontSize或Style属性时,我都希望收到通知.在 WPF中,我将通过调用DependencyProperty.OverrideMetadata()来执行此操作.当然,有用的东西在Silverlight中没有任何地方.那么,怎么可能会收到这种类型的通知

解决方法

我认为这是一个更好的方法.仍然需要看到利弊.

/// 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注册一个元素的属性的更改通知,如.

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

在同一个http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html上看到我的帖子

使用Silverlight 4.0测试版.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐