title: WPF usercontrol Twoway binding Dependency Property
body: <p>I created a Dependency Property in the usercontrol, but however changes in the usercontrol was NOT notified to the Viewmodel</p>
  
  <p>Usercontrol</p>
  
  <pre><code>&lt;UserControl x:Class="DPsample.UserControl1"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300"&gt;
  &lt;Grid&gt;
      &lt;TextBox x:Name="txtName"&gt;&lt;/TextBox&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>UserControl.cs</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Interaction logic for UserControl1.xaml
  /// &lt;/summary&gt;
  public partial class UserControl1 : UserControl
  {
      public UserControl1()
      {
          InitializeComponent();
      }
  
      #region SampleProperty 
  
      public static readonly DependencyProperty SamplePropertyProperty
                                              = DependencyProperty.Register("SampleProperty", 
                                              typeof(string), 
                                              typeof(UserControl1), 
                                              new PropertyMetadata(OnSamplePropertyChanged));
  
  
      public string SampleProperty
      {
          get { return (string)GetValue(SamplePropertyProperty); }
          set { SetValue(SamplePropertyProperty, value); }
      }
  
  
      static void OnSamplePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
      {
          (obj as UserControl1).OnSamplePropertyChanged(e);
      }
      private void OnSamplePropertyChanged(DependencyPropertyChangedEventArgs e)
      {
          string SamplePropertyNewValue = (string)e.NewValue;
  
          txtName.Text = SamplePropertyNewValue;
      }
  
      #endregion
  }
  </code></pre>
  
  <p>MainWindow</p>
  
  <pre><code>&lt;Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:DPsample" x:Class="DPsample.MainWindow"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
  
      &lt;local:UserControl1 SampleProperty="{Binding SampleText,Mode=TwoWay}" HorizontalAlignment="Left" Margin="76,89,0,0" VerticalAlignment="Top" Width="99"/&gt;
      &lt;Button Content="Show" HorizontalAlignment="Left" Margin="76,125,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>MainWindow.cs</p>
  
  <pre><code> public MainWindow()
      {
          InitializeComponent();
          this.DataContext = new MainViewModel();
      }
  
      private void Button_Click(object sender, RoutedEventArgs e)
      {
          var item = this.DataContext as MainViewModel;
          MessageBox.Show(item.SampleText.ToString());
      }
  </code></pre>
  
  <p>MainViewModel.cs</p>
  
  <pre><code>public class MainViewModel : NotifyViewModelBase
  {
      public MainViewModel()
      {
          this.SampleText = "test";           
      }
  
      private string _sampleText;
      public string SampleText
      {
          get { return _sampleText; }
          set { _sampleText = value; OnPropertyChanged("SampleText"); }
      }
  }
  </code></pre>