UserControl Dependency属性不起作用

我有我的UserControl。 我想设置Dependency属性,但我不能。

TextBoxExtended.cs

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}" 

MainWindow.xaml

  

我需要事件textchange,我使用此代码

  

但我需要在引入每个角色后进行更改

您必须在TextBoxExtended.xaml中设置Binding的源对象,例如

  

在TextBox的绑定中使用ElementName。