简体   繁体   中英

Bind Control to a User Control Property

I would like my user control to modify the text of a TextBlock in its parent. The parent control should be able to bind the TextBlock to a property in the user control.

The TextBlock is currently not binding.

Here is how I am currently trying to do this:

Parent Control:

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

<TextBlock x:Name="texttest"/>

User Control Code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM