.net - 当依赖属性发生变化时是否有通知机制?

标签 .net silverlight silverlight-2.0 dependency-properties

在 Silverlight 应用程序中,我试图找出用户控件上的属性何时更改。我对一个特定的 DependencyProperty 很感兴趣,但不幸的是控件本身没有实现 INotifyPropertyChanged。

是否有任何其他方法可以确定值是否已更改?

最佳答案

可以。至少我做到了。仍然需要查看优缺点。

 /// 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, (d, e) => MessageBox.Show("Value changed"));

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

关于.net - 当依赖属性发生变化时是否有通知机制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/240156/

相关文章:

c# - 无法加载文件或程序集 'EntityFramework' 或其依赖项之一

wpf - future 的桌面 LOB 应用程序——WPF、Silverlight 或……HTML5?

silverlight - 输入一天中的时间或持续时间的最直观、最有用的方法是什么?

datagrid - Silverlight DataGrid 设置 ItemSource/DataContext 时自动选择,排序,

c# - Silverlight 中的验证

.net - 对单字符值使用 char 而不是 string 有什么好处吗?

c# - 使用 C# 访问私有(private) Google 电子表格

.net 运行时 - Silverlight 运行时 =?

.net - 使用单个 exe 部署 Windows 窗体应用程序

silverlight - 如何暂时禁用 Silverlight 3 导航栏中的页面链接?