title: Binding between my usercontrol and ViewModel
body: <p>Can not do Binding</p>
  
  <p>Random (MyViewModel) -> VMTestValue (MyViewModel) -> TestUserControl (MainWindow)</p>
  
  <p>"MyViewModel" is ViewModel for "MainWindow"</p>
  
  <p>All project: <a href="http://rusfolder.com/32608140" rel="nofollow">http://rusfolder.com/32608140</a></p>
  
  <p>TestUserControl.xaml</p>
  
  <pre><code>&lt;UserControl x:Class="TestBinding.TestUserControl"
               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" 
               Background="Green"
               d:DesignHeight="300" d:DesignWidth="300"&gt;
      &lt;Grid&gt;
          &lt;TextBlock Background="Gray" Margin="50" FontSize="18" Text="{Binding TestValue}" /&gt;
          &lt;Rectangle  Height="200" Width="{Binding TestValue}" Fill="#FFFF1717" /&gt;
      &lt;/Grid&gt;
   &lt;/UserControl&gt;
  </code></pre>
  
  <p>TestUserControl.cs</p>
  
  <pre><code>...
  public TestUserControl()
          {
              InitializeComponent();
  
              this.DataContext = this;
          }
  
          public static readonly DependencyProperty TestValueProperty =
             DependencyProperty.Register("TestValue", typeof(int), typeof(TestUserControl),
                 new FrameworkPropertyMetadata((int)255)
                 );
  
          public int TestValue
          {
              set { SetValue(TestValueProperty, value); }
              get
              {
                  return (int)GetValue(TestValueProperty);
              }
          }
  ...
  </code></pre>
  
  <p>MyViewModel.cs</p>
  
  <pre><code>using System;
  
  using System.Timers;
  using Microsoft.Practices.Prism.ViewModel;
  
  namespace TestBinding
  {
      class MyViewModel : NotificationObject
      {
          private int _VMTestValue;
  
          private Timer timer;
          private Random _random;
  
          public MyViewModel()
          {
              _random=new Random();
  
              timer=new Timer();
              timer.Interval = 500;
              timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
              timer.Start();
          }
  
          void timer_Elapsed(object sender, ElapsedEventArgs e)
          {
              VMTestValue = _random.Next(10, 300);
          }
  
          public int VMTestValue
          {
              set 
              { 
                  _VMTestValue = value;
                  this.RaisePropertyChanged(() =&gt; this.VMTestValue);
              }
              get { return _VMTestValue; }
          }
      }
  }
  </code></pre>
  
  <p>MainWindow.cs</p>
  
  <pre><code>...
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              this.DataContext = new MyViewModel();
          }
      }
  ...
  </code></pre>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>...
  &lt;TestBinding1:TestUserControl TestValue="{Binding VMTestValue}" /&gt;
  &lt;TextBlock VerticalAlignment="Bottom" Background="Blue" FontSize="20" Text="{Binding VMTestValue}" /&gt;
  ...
  </code></pre>
  
  <p><strong>Why can not I go to the UserControl?</strong></p>