- Mastering Windows Presentation Foundation
- Sheridan Yuen
- 226字
- 2021-06-24 16:49:04
Catching changing Dependency Property values
As we saw at the beginning of this chapter, the WPF Framework won't call the CLR property wrappers of our Dependency Properties when the property values are changing. However, there is a way to accomplish this using callback handlers. In fact, we've already seen an example of this when we were looking at the creation of the OnEnterKeyDown Attached Property. Let's remind ourselves what that looked like:
public static DependencyProperty OnEnterKeyDownProperty = DependencyProperty.RegisterAttached("OnEnterKeyDown",
typeof(ICommand), typeof(TextBoxProperties),
new PropertyMetadata(OnOnEnterKeyDownChanged));
...
public static void OnOnEnterKeyDownChanged(
DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { TextBox textBox = (TextBox)dependencyObject; if (e.OldValue == null && e.NewValue != null) textBox.PreviewKeyDown += TextBox_OnEnterKeyDown; else if (e.OldValue != null && e.NewValue == null) textBox.PreviewKeyDown -= TextBox_OnEnterKeyDown; }
For this Attached Property, we used a particular overload of the DependencyProperty.RegisterAttached method that accepts a PropertyMetadata object, which enabled us to assign a PropertyChangedCallback handler to the property. Note that there is an identical overload for the DependencyProperty.Register method for declaring Dependency Properties.
Program execution will enter these PropertyChangedCallback handlers each time their related Dependency Property changes, so that makes them perfect for debugging their values. While we don't often need to attach these handlers, it only takes a moment to add one when we need to and they enable us to find out what's going on with the Dependency Property values at runtime.
- Data Visualization with D3 4.x Cookbook(Second Edition)
- Implementing Modern DevOps
- ExtGWT Rich Internet Application Cookbook
- Designing Machine Learning Systems with Python
- ThinkPHP 5實戰
- Java高并發核心編程(卷2):多線程、鎖、JMM、JUC、高并發設計模式
- 深入淺出Prometheus:原理、應用、源碼與拓展詳解
- Wireshark Network Security
- C語言程序設計案例精粹
- Kotlin從基礎到實戰
- 單片機原理及應用技術
- Python Deep Learning
- Penetration Testing with the Bash shell
- Python網絡爬蟲實例教程(視頻講解版)
- Arduino Electronics Blueprints