C# 绑定到UserControl中的属性
我有一个usercontrol,我想在其中公开一个名为ExpressionText的属性,并在 xaml可以为此属性定义绑定。 所以我创建了一个依赖属性C# 绑定到UserControl中的属性,c#,wpf,data-binding,user-controls,dependency-properties,C#,Wpf,Data Binding,User Controls,Dependency Properties,我有一个usercontrol,我想在其中公开一个名为ExpressionText的属性,并在 xaml可以为此属性定义绑定。 所以我创建了一个依赖属性 public static readonly DependencyProperty EditorText =DependencyProperty.Register("EditorText", typeof(string), typeof(MyUerControl)); 及 在xaml中,我这样做 <controls:MyUer
public static readonly DependencyProperty EditorText =DependencyProperty.Register("EditorText", typeof(string), typeof(MyUerControl));
及
在xaml中,我这样做
<controls:MyUerControl x:Name="textEditor" ExpressionText="{Binding
Path=Expression,Mode=TwoWay}" />
但我明白了
无法在MyUserControl类型的ExpressionText属性上设置绑定。可以设置绑定
仅依赖于类型为Dependency object error的DependedEcy属性
我的方法有问题吗?如何解决此问题?您正在将EditorText定义为DependencyProperty的名称。这是您可以公开绑定到的名称。如果希望将其命名为ExpressionText,则需要将其注册为名称
public static readonly DependencyProperty EditorText =
DependencyProperty.Register("ExpressionText", typeof(string), typeof(MyUerControl));
您正在将EditorText定义为DependencyProperty的名称。这是您可以公开绑定到的名称。如果希望将其命名为ExpressionText,则需要将其注册为名称
public static readonly DependencyProperty EditorText =
DependencyProperty.Register("ExpressionText", typeof(string), typeof(MyUerControl));
这应该起作用:
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);
}
}
这应该起作用:
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);
}
}
必须说我很惊讶我的答案被接受:P杰夫解释了你做错了什么,我只是展示了如何修复:)必须说我很惊讶我的答案被接受:P杰夫解释了你做错了什么,我只是展示了如何修复:)