title: WPF Data binding with ResourceDictionary MVVM
body: <p>I'm trying to bind a View with a ViewModel within ResourceDictionary but it does not work.</p>
  
  <p>The application is very simple window with 2 textboxes. When I type text to textbox1, atutomatically, textbox2 must get the same text. Of course my textboxes from the View have to be binded to my properties in ViewModel.</p>
  
  <p>I'm new to WPF and the way I started to bind Views and ViewModels was in the codebehind of a View: </p>
  
  <pre><code>DataContext = new MyViewModel();
  </code></pre>
  
  <p>Now I'm trying to achieve a cleaner separation. My code is</p>
  
  <p>App.xaml:</p>
  
  <pre><code>&lt;Application x:Class="NavigationCleanBinding.App"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           StartupUri="/Views/MainWindowView.xaml"&gt;
      &lt;Application.Resources&gt;
          &lt;ResourceDictionary Source="MainResourceDictionary.xaml" /&gt;
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p>MainResourceDictionary.xaml:</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xamlpresentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:Views="clr-namespace:NavigationCleanBinding.Views"
      xmlns:ViewModels="clr-namespace:NavigationCleanBinding.ViewModels"&gt;
  
      &lt;DataTemplate DataType="{x:Type ViewModels:MainWindowViewModel}"&gt;
          &lt;Views:MainWindowView /&gt;
      &lt;/DataTemplate&gt;
  
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>MainWindowView.xaml:</p>
  
  <pre><code>&lt;Window x:Class="NavigationCleanBinding.Views.MainWindowView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525"&gt;
  
  &lt;Grid&gt;
      &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="61,14,0,0" 
               Name="textBox1" VerticalAlignment="Top" Width="120" 
               Text="{Binding TestData, Mode=TwoWay,
               UpdateSourceTrigger=PropertyChanged}"/&gt;
      &lt;Label Content="Test:" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0"
        Name="label1" VerticalAlignment="Top" Width="43" /&gt;
      &lt;Label Content="Result:" Height="28" HorizontalAlignment="Left" Margin="10,46,0,0"
        Name="label2" VerticalAlignment="Top" /&gt;
  
      &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="61,48,0,0"
               Name="textBox2" VerticalAlignment="Top" Width="120" 
  
               Text="{Binding TestData, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainWindowViewModel:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace NavigationCleanBinding.ViewModels
  {
      class MainWindowViewModel
      {
          private String _testData;
          public String TestData
          {
              get { return _testData; }
              set { _testData = value; }
          }
  
          private MainWindowViewModel()
          {
              _testData = null;
          }
      }
  }
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>I changed property TestData to this:</p>
  
  <pre><code>public String TestData
      {
          get { return _testData; }
          set
          { 
              _testData = value;
              OnPropertyChanged("TestData");
  
          }
      }
  </code></pre>
  
  <p>And implemened the INotifyPropertyChanged like this:</p>
  
  <pre><code>public event PropertyChangedEventHandler PropertyChanged;
      private void OnPropertyChanged(String propertyName)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  </code></pre>