Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UserControl依赖项属性不起作用_C#_Wpf_User Controls - Fatal编程技术网

C# UserControl依赖项属性不起作用

C# 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

我有我的用户控制。我想设置依赖项属性,但我不能

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