0

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) { } 
7
  • Do you have any binding errors when run in Output? Commented Oct 19, 2017 at 4:31
  • RelativeSource is probably necessary Commented Oct 19, 2017 at 4:37
  • @mvermef I can't see any errors in Output. Do you have any examples of RelativeSource? Commented Oct 19, 2017 at 4:53
  • 1
    This looks odd. Why don't you simply bind the TextBlock's Text property to a string property of your UserControl? Commented Oct 19, 2017 at 5:36
  • 1
    Overkill? Not at all. Just change the type of your dependency property from TextBlock to string. You would thus be able to bind to other control types besides TextBlock, like TextBox, Label, etc. Commented Oct 19, 2017 at 6:06

1 Answer 1

2
<localcontrols:MyControl TextName="{Binding ElementName=texttest}"/> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.