title: Binding to dependency and regular properties in WPF
body: <p>I started learning WPF few days ago and have been creating some test projects to evaluate my understanding of the materials as I learn them. According to <a href="http://www.codeproject.com/Articles/18270/A-Guided-Tour-of-WPF-Part-3-Data-binding" rel="nofollow">this article</a>, "a property can only be bound if it is a dependency property." To test this, I used two different patterns: </p>
  
  <p><strong>1. Using MVVM pattern (sort of)</strong></p>
  
  <p>I created the <code>Movie</code> model:</p>
  
  <pre><code>public class MovieModel
  {
      private string _movieTitle;
      private string _rating;
  
      public string MovieTitle
      {
          get { return _movieTitle; }
          set { _movieTitle = value; }
      }
  
      public string Rating
      {
          get { return _rating; }
          set { _rating = value; }
      }
  }
  </code></pre>
  
  <p>and the <code>MovieViewModel</code> view model:</p>
  
  <pre><code>public class MovieViewModel
  {
      MovieModel _movie;
  
      public MovieViewModel()
      {
          _movie = new MovieModel { MovieTitle = "Inception (Default)" };
      }
  
      public MovieModel Movie
      {
          get { return _movie; }
          set { _movie = value; }
      }
  
      public string MovieTitle
      {
          get { return Movie.MovieTitle; }
          set { Movie.MovieTitle = value; }
      }
  }
  </code></pre>
  
  <p>And finally in the my main <code>View</code>, I set the DataContext to an instance of the <code>MovieViewModel</code> class:</p>
  
  <pre><code>&lt;Window x:Class="MVVMTestProject.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:MVVMTestProject.ViewModels"
      Name="MainWindowElement"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Window.DataContext&gt;
      &lt;local:MovieViewModel/&gt;
  &lt;/Window.DataContext&gt;
  &lt;Grid&gt;
      &lt;StackPanel Orientation="Vertical"&gt;
          &lt;TextBox Width="150" Text="{Binding Path=MovieTitle}"&gt;&lt;/TextBox&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>This works fine, when I run the application I see <code>Inception (Default)</code> in the textbox, even though none of the properties in the <code>Model</code> or <code>ViewModel</code> are <code>DependencyProperties</code>. Now the second try was by:</p>
  
  <p><strong>2. Using a property in the code behind of <code>MainView</code></strong></p>
  
  <p>In the code behind of my main view, I put:</p>
  
  <pre><code>private string _myText;
  
  public MainWindow()
  {
      InitializeComponent();
      MyText = "Some Text";
  }
  
  public string MyText
  {
      get { return _myText; }
      set { set _myText = value; }
  }
  </code></pre>
  
  <p>And then I changed my view to:</p>
  
  <pre><code>&lt;Window x:Class="MVVMTestProject.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:MVVMTestProject.ViewModels"
      Name="MainWindowElement"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Window.DataContext&gt;
      &lt;local:MovieViewModel/&gt;
  &lt;/Window.DataContext&gt;
  &lt;Grid&gt;
      &lt;StackPanel Orientation="Vertical"&gt;
          &lt;TextBox Width="150" Text="{Binding ElementName=MainWindowElement, Path=MyText}"&gt;&lt;/TextBox&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>But that didn't work. When I run the application, the textbox is blank. So I changed the property to a dependency property, and oddly enough, it worked. So I don't understand why the properties on the Model and ViewModel can be regular properties, but the code-behind properties have to be dependency properties in order to bind to? Is it because the ViewModel is set to the DataContext of the view and the parser is smart enough to realize this or is there another reason?</p>
  
  <p>Also a not-very-relevant question: I noticed in almost every singe article on WPF people use a local variable and then define a getter and setter for it, even though we all know in .NET 2.5 (I believe?) and above, we can just use the <code>get; set;</code> syntax without having to define a local member variable. So is there a specific reason for this?</p>
  
  <p>Thanks in advance.</p>