# User Controls, Dependency Properties, TwoWay Binding, and Hair Pulling!

**URL:** <https://forum.kirupa.com/t/user-controls-dependency-properties-twoway-binding-and-hair-pulling/298066>\
**Category:** Uncategorized\
**Created:** [October 13, 2009, 10:32am UTC](https://forum.kirupa.com/t/user-controls-dependency-properties-twoway-binding-and-hair-pulling/298066 "2009-10-13T10:32:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ETFairfax](https://avatars.discourse-cdn.com/v4/letter/e/e274bd/32.png) [@ETFairfax](https://forum.kirupa.com/u/ETFairfax)\
**Post date:** [October 13, 2009, 10:32am UTC](https://forum.kirupa.com/t/user-controls-dependency-properties-twoway-binding-and-hair-pulling/298066/1 "2009-10-13T10:32:20Z")

</div>

Hi,

I have this user control…

\<UserControl x:Class=“SilverlightApplication.UserControls.FormItem\_TextBox”  
xmlns=“[http://schemas.microsoft.com/winfx/2006/xaml/presentation](http://schemas.microsoft.com/winfx/2006/xaml/presentation)”  
xmlns:x=“[http://schemas.microsoft.com/winfx/2006/xaml](http://schemas.microsoft.com/winfx/2006/xaml)”  
\>

```
&lt;StackPanel Orientation="Vertical" &gt;

    &lt;TextBlock x:Name="lbCaption" Style="{StaticResource TextBlockStyle}" /&gt;
    &lt;TextBox x:Name="tbItem" Style="{StaticResource TextBoxStyle}" /&gt;

&lt;/StackPanel&gt;

```

\</UserControl\>

…which has this code behind…

public partial class FormItem\_TextBox : UserControl  
{

```
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(FormItem_TextBox), new PropertyMetadata(TextChanged));
    public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(String), typeof(FormItem_TextBox), new PropertyMetadata(CaptionChanged));

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

    public String Caption
    {
        get { return (String)GetValue(CaptionProperty); }
        set { SetValue(CaptionProperty, value); }
    }

    private static void TextChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        FormItem_TextBox item = (source as FormItem_TextBox);        
        item.tbItem.Text = e.NewValue.ToString();            
    }

    private static void CaptionChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {    
        FormItem_TextBox item = (source as FormItem_TextBox);
        item.lbCaption.Text = e.NewValue.ToString();
    }

    public FormItem_TextBox()
    {
        InitializeComponent();
    }

}

```

How comes that when I attempt to TwoWay databind to the controls TextProperty, like so…

\<UC:FormItem\_TextBox Caption=“First Name: " Text=”{Binding Path=FirstName, Mode=TwoWay}" /\>

…it doesn’t play ball??

I know my MVVM stuff is OK because I have tested it on a simple text box, i.e.

\<TextBox Text="{Binding Path=FirstName, Mode=TwoWay}" /\>

…and it works just fine.

Is there any other wiring up that is needed within my user control??

I am brand spanking new to silverlight/WPF, so please be gentle!!!

Many thanks for any responses.

ETFairfax
