Skip to main content

Microsoft Silverlight

Answered Question TextBox.Text won't bind trough a dependency propertyRSS Feed

(0)

mrjvdveen
mrjvdveen

Participant

Participant

1937 points

366 Posts

TextBox.Text won't bind trough a dependency property

Here is what I'm trying to do. I want to have a control that combines a TextBlock to display a label and a TextBox to display/edit a value. As this combines two basic controls, I figured I would go with a UserControl. The Xaml for it is very easy:

    <StackPanel x:Name="stackPanel">
        <TextBlock x:Name="labelTextBlock" Style="{StaticResource LabeledTextBoxLabel}"/>
        <TextBox x:Name="textBox" Style="{StaticResource LabeledTextBoxTextBox}"/>
    </StackPanel>

All is still well.

I've added some dependency properties to my new LabeledTextBox UserControl, to allow setting some properties on the underlying controls. One of these properties is the Text property:

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set
            {
                SetValue(TextProperty, value);
                UpdateText(value);
            }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(LabeledTextBox), new PropertyMetadata(string.Empty));

The UpdateText(value) line only passes the value to the textBox.Text property. Now in my application I want to databind to the Text property of my UserControl, which in turn should pass the binding to the textBox inside. It doesn't databind. It doesn't show any initial values.

Help?

-------------
Please mark a post as answer if it answers your question.
Visit my blog: http://jvdveen.blogspot.com

Ag.
Ag.

Member

Member

101 points

19 Posts

Answered Question

Re: TextBox.Text won't bind trough a dependency property

You should call UpdateText in the property changed callback not in the setter 

public string Text
{
    get { return (string) GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
}

public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register(
        "Text", typeof (string), typeof (LabeledTextBox),
        new PropertyMetadata(string.Empty, OnTextChanged));

private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var labeledTextBox = d as LabeledTextBox;
    if (labeledTextBox == null) return;
    labeledTextBox.UpdateText(e.NewValue as string);
}
 

Best Regards

Valery Sarkisov

mrjvdveen
mrjvdveen

Participant

Participant

1937 points

366 Posts

Re: TextBox.Text won't bind trough a dependency property

 That does actually work! Thanks.

However I don't realy understand why my solution does work for a usercontrol wrapping a combobox. I used exactly the same method for passing on bindings to the selecteditem property and it works fine. Any insights?

-------------
Please mark a post as answer if it answers your question.
Visit my blog: http://jvdveen.blogspot.com
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities