هل هناك آلية الإخطار عندما تبعية الملكية قد تغير ؟
-
04-07-2019 - |
سؤال
في تطبيق Silverlight أحاول معرفة عندما الملكية على usercontrol قد تغير.أنا مهتم في معين واحد DependencyProperty ، ولكن للأسف تحكم نفسها لا تنفذ INotifyPropertyChanged.
هل هناك أي طريقة أخرى لتحديد ما إذا كانت القيمة قد تغير ؟
المحلول
في WPF لديك DependencyPropertyDescriptor.AddValueChanged, ولكن للأسف في سيلفرلايت لا يوجد شيء من هذا القبيل.وبالتالي فإن الجواب هو لا.
ربما لو شرح ما كنت تحاول أن تفعل يمكنك حل هذا الوضع ، أو استخدام الارتباطات.
نصائح أخرى
ويمكنك. أتلست فعلت. لا تزال بحاجة لمعرفة إيجابيات وسلبيات.
/// 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 / سيلفرلايت الاستماع إلى dependency.html
وكما شارك جون غالاوي في موضوع آخر، قد تكون قادرا على استخدام شيء من هذا القبيل WeakReference التفاف خصائص كنت مهتما في وإعادة تسجيلهم في الفئة الخاصة بك. هذا هو رمز WPF ولكن هذا المفهوم لا تعتمد على DependencyPropertyDescriptor.
وراجع الرابط التالي. ومن showns كيفية الحصول على حل المشكلة في Silverlight حيث لم يكن لديك DependencyPropertyDescriptor.AddValueChanged
http://themechanicalbride.blogspot.com/2008 /10/building-observable-model-in.html