title: MVVM Binding to Custom Control in Windows Phone
body: <p>I have the following simple custom control that I have defined for a Windows Phone Application. The XAML for the control is as follows : </p>
  
  <pre><code>&lt;UserControl x:Class="PhoneApp1.MyControl"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
  FontFamily="{StaticResource PhoneFontFamilyNormal}"
  FontSize="{StaticResource PhoneFontSizeNormal}"
  Foreground="{StaticResource PhoneForegroundBrush}"
           d:DesignHeight="480"
           d:DesignWidth="480"
           DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt;
  
  &lt;Grid x:Name="LayoutRoot"&gt;
      &lt;TextBlock Text="{Binding MyLabel}" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>In addition the user control has a dependency property which looks as follows :</p>
  
  <pre><code>public partial class MyControl : UserControl
  {
      public MyControl()
      {
          InitializeComponent();
      }
  
      public static readonly DependencyProperty MyLabelProperty =
          DependencyProperty.Register("MyLabel", typeof (string), typeof (MyControl), new PropertyMetadata(default(string)));
  
      public string MyLabel
      {
          get { return (string) GetValue(MyLabelProperty); }
          set { SetValue(MyLabelProperty, value); }
      }
  }
  </code></pre>
  
  <p>Now the thing is when I try to databind it to a View Model that I have created, nothing happens. The various code items are as follows :</p>
  
  <pre><code>public class MyControlViewModel : ViewModelBase
  {
      private string name;
  
      public string Name
      {
          get { return name; }
          set { name = value;
          RaisePropertyChanged(() =&gt; Name);}
      }
  }
  </code></pre>
  
  <p>View Model declare in App.xaml</p>
  
  <pre><code>&lt;Application.Resources&gt;
      &lt;PhoneApp1:MyControlViewModel x:Key="MCVM" /&gt;
  &lt;/Application.Resources&gt;
  </code></pre>
  
  <p>MainPage.xaml declaration of the control</p>
  
  <pre><code>&lt;Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"&gt;
          &lt;PhoneApp1:MyControl  x:Name="testControl" DataContext="{StaticResource MCVM}" MyLabel="{Binding Path=MyLabel}" /&gt;            
      &lt;/Grid&gt;
  </code></pre>
  
  <p>However, if I try to databind it to some other UI Element as follows,it seems to work?</p>
  
  <pre><code>&lt;Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"&gt;
          &lt;PhoneApp1:MyControl  x:Name="testControl" MyLabel="{Binding ElementName=Button, Path=Content}" /&gt;
          &lt;Button Click="Button_Click" x:Name="Button" Content="Click" /&gt;
      &lt;/Grid&gt;
  </code></pre>
  
  <p>I have been struggling to get this simple scenario to work, and I think that there is something silly that I'm missing?</p>
  
  <p>Any help would be greatly appreciated</p>