我有一个用户控件,我想在其中公开一个名为 ExpressionText 的属性,并在 可以为此属性定义一个 xaml 绑定(bind)。 所以我创建了一个依赖属性
public static readonly DependencyProperty EditorText =DependencyProperty.Register("EditorText", typeof(string), typeof(MyUerControl));
和
public string ExpressionText
{
get
{
return (string)GetValue(EditorText);
}
set
{
SetValue(EditorText, value);
}
}
我在 xaml 中这样做。
<controls:MyUerControl x:Name="textEditor" ExpressionText="{Binding
Path=Expression,Mode=TwoWay}" />
但是我明白了
无法在 MyUserControl 类型的 ExpressionText 属性上设置绑定(bind)。可以设置绑定(bind) 仅在 Dependency 对象类型错误的 depenedecy 属性上。
我的方法有问题吗?我该如何解决这个问题?
请您参考如下方法:
这应该有效:
public static DependencyProperty EditorTextProperty = DependencyProperty.Register("ExpressionText", typeof(string), typeof(MyUserControl),
new PropertyMetadata(new PropertyChangedCallback((s, e) =>
{ })));
public string ExpressionText
{
get
{
return (string)base.GetValue(EditorTextProperty);
}
set
{
base.SetValue(EditorTextProperty, value);
}
}
