2

Silverlight アプリケーションで、ユーザー コントロールのプロパティがいつ変更されたかを調べようとしています。特定の DependencyProperty に興味がありますが、残念ながらコントロール自体は INotifyPropertyChanged を実装していません。

値が変更されたかどうかを判断する他の方法はありますか?

4

4 に答える 4

5

あなたはできる。少なくとも私はしました。まだ長所と短所を確認する必要があります。

 /// 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の私の投稿を参照してください

于 2009-12-02T18:59:10.397 に答える
2

In WPF you have DependencyPropertyDescriptor.AddValueChanged, but unfortunately in Silverlight there's no such thing. So the answer is no.

Maybe if you explain what are you trying to do you can workaround the situation, or use bindings.

于 2008-10-27T15:05:47.547 に答える
1

Jon Galloway が別のスレッドに投稿したように、WeakReference のようなものを使用して、関心のあるプロパティをラップし、独自のクラスに再登録できる場合があります。これは WPF コードですが、概念は DependencyPropertyDescriptor に依存していません。

記事リンク

于 2008-11-03T13:11:51.187 に答える
0

次のリンクをチェックしてください。DependencyPropertyDescriptor.AddValueChangedがないSilverlightで問題を回避する方法を示しています

http://themechanicalbride.blogspot.com/2008/10/building-observable-model-in.html

于 2009-03-18T21:56:49.167 に答える