更新时间:2022-06-05 02:56:18
您的依赖项属性声明错误.它必须看起来如下所示,其中CLR属性包装器的getter和setter调用GetValue和SetValue方法:
Your dependency property declaration is wrong. It has to look like shown below, where the getter and setter of the CLR property wrapper call the GetValue and SetValue methods:
public static readonly DependencyProperty TextBoxTextProperty =
DependencyProperty.Register(
"TextBoxText", typeof(string), typeof(TextBoxUnitConvertor));
public string TextBoxText
{
get { return (string)GetValue(TextBoxTextProperty); }
set { SetValue(TextBoxTextProperty, value); }
}
在UserControl的XAML中,您将这样绑定到属性:
In the XAML of your UserControl, you would bind to the property like this:
<TextBox Text="{Binding TextBoxText,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
如果每当TextBoxText
属性发生更改时需要通知,则可以使用传递给Register方法的PropertyMetadata来注册PropertyChangedCallback:
If you need to get notified whenever the TextBoxText
property changes, you could register a PropertyChangedCallback with PropertyMetadata passed to the Register method:
public static readonly DependencyProperty TextBoxTextProperty =
DependencyProperty.Register(
"TextBoxText", typeof(string), typeof(TextBoxUnitConvertor),
new PropertyMetadata(TextBoxTextPropertyChanged));
private static void TextBoxTextPropertyChanged(
DependencyObject o, DependencyPropertyChangedEventArgs e)
{
TextBoxUnitConvertor t = (TextBoxUnitConvertor)o;
t.CurrentValue = ...
}