title: DependencyProperty ... can set, but can't get value out via binding
body: <p>I have a Window, containing a UserControl 'TemplateEditor'. The (shortened) TemplateEditor XAML is:</p>
  
  <pre><code>&lt;UserControl x:Class="xxx.Windows.Core.Controls.TemplateEditor"
               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" 
               x:Name="userControl"&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="*" /&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;TextBox Grid.Row="1" x:Name="textBox" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" /&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>I want to be able to bind data through the TemplateEditor into the TextBox "textBox". I'm using a DependencyProperty to mask the TextBox in the code-behind:</p>
  
  <pre><code>namespace xxx.Windows.Core.Controls
  {
      public partial class TemplateEditor : UserControl 
      {
          public string Text
          {
              get 
              { 
                  string s=(string)GetValue(TextProperty);
                  return s;
              }
              set 
              {
                  if (Text != value)
                  { 
                      SetValue(TextProperty, value);
                  }
              }
          }
  
          public static readonly DependencyProperty TextProperty =
              DependencyProperty.Register("Text", typeof(string), typeof(TemplateEditor),new PropertyMetadata(null,Text_PropertyChanged));
  
          public TemplateEditor()
          {
              InitializeComponent();
          }
  
          private static void Text_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
          {
              ((TemplateEditor)source).textBox.Text = (string)e.NewValue;
          } 
      }
  }
  </code></pre>
  
  <p>So from that you can see I have a DependencyProperty Text that I have a callback on to pick up changes made by to the binding (say, from the ViewModel) and apply to the TextBox value.</p>
  
  <p>This works.</p>
  
  <p>The problem is, I can't seem to have the binding work in reverse, ie. to get the value back out of the Text property (and therefore the TextBox) and back in to the binding consumer (the ViewModel). I've debugged calling GetValue(TextProperty) and this returns the correct value so the DP dictionary is correctly updating.</p>
  
  <p>Given the following Binding in the parent Window's XAML:</p>
  
  <pre><code>&lt;src:ApplicationWindowBase x:Class="xxx.Windows.Client.Filing"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:src="clr-namespace:xxx.Windows.Core;assembly=MIGTurbo1.Windows.Core"
          xmlns:viewmodel="clr-namespace:xxx.Windows.Client.ViewModel"
          xmlns:converters="clr-namespace:xxx.Windows.Client.Converters"
          xmlns:xxx_Windows_Core_Controls="clr-namespace:xxx.Windows.Core.Controls;assembly=xxx.Windows.Core" 
          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" 
          Title="File Item(s)" Height="450" Width="550" ShowInTaskbar="False"&gt;
      &lt;src:ApplicationWindowBase.Resources&gt;
          &lt;viewmodel:ViewModelLocator x:Key="ViewModelLocator" d:IsDataSource="True"/&gt;
          &lt;converters:FileableItemNameStringConverter x:Key="fileableItemNameStringConverter" /&gt;
          &lt;converters:FileableItemTypeStringConverter x:Key="fileableItemTypeStringConverter" /&gt;
          &lt;converters:FileableItemMetaDataStringConverter x:Key="fileableItemMetaDataStringConverter" /&gt;
          &lt;converters:FileableItemIconConverter x:Key="fileableItemIconConverter" /&gt;
      &lt;/src:ApplicationWindowBase.Resources&gt;
      &lt;src:ApplicationWindowBase.DataContext&gt;
          &lt;Binding Mode="OneWay" Path="Filing" Source="{StaticResource ViewModelLocator}"/&gt;
      &lt;/src:ApplicationWindowBase.DataContext&gt;
      &lt;Grid&gt;
         &lt;!-- SNIP --&gt;
      &lt;xxx_Windows_Core_Controls:TemplateEditor Grid.Column="1" Grid.Row="1" Margin="0" Text="{Binding Comment, Mode=TwoWay}" d:LayoutOverrides="Width, Height"/&gt;
         &lt;!-- SNIP --&gt;
  &lt;/src:ApplicationWindowBase&gt;
  </code></pre>
  
  <p>I am using MVVM Light and the ViewModel does bind correctly. There are other control/field bindings (ommitted) that work fine. THe problem is that the Text property binding on the Comment ViewModel property is not working. I can set it fine (ie. set value in ViewModel, then bind) but the user-entered value in Text never goes into the ViewModel Comments property.</p>
  
  <p>What am I doing wrong?</p>