C# UserControl依赖项属性不起作用
我有我的用户控制。我想设置依赖项属性,但我不能 TextBoxExtended.csC# UserControl依赖项属性不起作用,c#,wpf,user-controls,C#,Wpf,User Controls,我有我的用户控制。我想设置依赖项属性,但我不能 TextBoxExtended.cs public partial class TextBoxExtended : UserControl { public static readonly DependencyProperty MyTextPropertyProperty = DependencyProperty.Register("MyTextProperty", typeof(string), typeo
public partial class TextBoxExtended : UserControl
{
public static readonly DependencyProperty MyTextPropertyProperty =
DependencyProperty.Register("MyTextProperty", typeof(string), typeof(TextBoxExtended), new UIPropertyMetadata(String.Empty));
public string MyTextProperty
{
get { return (string)GetValue(MyTextPropertyProperty); }
set { SetValue(MyTextPropertyProperty, value); }
}
TextBoxExtended.xaml
<TextBox x:Name="tbMain" Text="{Binding MyTextProperty}"
<local:TextBoxExtended x:Name="TextBoxSearch" Height="30" Margin="83,38,193,285" MyTextProperty="MyTextProperty"/>
您必须在TextBoxExtended.xaml中设置绑定的源对象,例如
<TextBox x:Name="tbMain"
Text="{Binding MyTextProperty,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
在文本框的绑定中使用ElementName
现在您没有文本框控件的源代码,这就是为什么它不起作用的原因。
<TextBox x:Name="tbMain"
Text="{Binding MyTextProperty,
RelativeSource={RelativeSource AncestorType=UserControl}}" />