将控件绑定到用户控件属性

问题描述:

我希望我的用户控件在其父项中修改TextBlock的文本。父控件应该能够将TextBlock绑定到用户控件中的属性。将控件绑定到用户控件属性

TextBlock目前没有约束力。

这里是我当前如何试图做到这一点:

家长控制:

<localcontrols:MyControl TextName="{Binding texttest}"/> 

<TextBlock x:Name="texttest"/> 

用户控制代码:

public static readonly DependencyProperty TextNameProperty = 
    DependencyProperty.Register("TextName", typeof(TextBlock), typeof(MyControl), new PropertyMetadata((TextBlock)null, MyControl.TextNameValueChanged)); 

public TextBlock TextName 
{ 
    get 
    { 
     return (TextBlock)this.GetValue(MyControl.TextNameProperty); 
    } 
    set 
    { 
     this.SetValue(MyControl.TextNameProperty, value); 
    } 
} 

private static void TextNameValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
} 
+0

你有什么在输出中运行时发生绑定错误? – mvermef

+0

RelativeSource可能是必需的 – mvermef

+0

@mvermef我在输出中看不到任何错误。你有没有关于RelativeSource的例子? – rhughes

<localcontrols:MyControl TextName="{Binding ElementName=texttest}"/>