且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

绑定到用户控件的依赖项属性

更新时间:2022-04-12 18:30:28

在XAML中(或通过绑定或动画等)设置依赖项属性时,WPF无需调用CLR包装器即可直接访问基础DependencyObject和DependencyProperty.请参见 XAML加载和依赖项属性, 自定义依赖项属性的含义.

When a dependency property is set in XAML (or by binding or animation etc.), WPF directly accesses the underlying DependencyObject and DependencyProperty without calling the CLR wrapper. See XAML Loading and Dependency Properties, Implications for Custom Dependency Properties.

为了获得有关Number属性更改的通知,您必须注册

In order to get notified about changes of the Number property, you have to register a PropertyChangedCallback:

public static readonly DependencyProperty NumberProperty =  
    DependencyProperty.Register("Number", 
        typeof(double), 
        typeof(FormattedTextBox), 
        new FrameworkPropertyMetadata(NumberPropertyChanged));

private static void NumberPropertyChanged(
    DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    var textBox = obj as FormattedTextBox;
    ...
}