将控件绑定到用户控件属性
问题描述:
我希望我的用户控件在其父项中修改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)
{
}
答
<localcontrols:MyControl TextName="{Binding ElementName=texttest}"/>
你有什么在输出中运行时发生绑定错误? – mvermef
RelativeSource可能是必需的 – mvermef
@mvermef我在输出中看不到任何错误。你有没有关于RelativeSource的例子? – rhughes