title: WPF Binding ViewModel property to Attached Property
body: <p>My goal is to create add Text property to RichTextBox. I created Attached Property and set binding to ViewModel's property. Unfortunately changing text in RichTextBox doesn't update underlying property.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>View.cs:</strong></p>
  
  <pre><code>public partial class KnuthMorrisPrattView : UserControl
  {
      public KnuthMorrisPrattView()
      {
          InitializeComponent();
      }
  
      public static string GetText(DependencyObject obj)
      {
          return (string)obj.GetValue(TextProperty);
      }
  
      public static void SetText(DependencyObject obj, string value)
      {
          obj.SetValue(TextProperty, value);
      }
  
      public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached
      (
          "Text",
          typeof(string),
          typeof(KnuthMorrisPrattView),
          new FrameworkPropertyMetadata()
          {
              BindsTwoWayByDefault = true,
              PropertyChangedCallback = PropertyChangedCallback,
              CoerceValueCallback = CoerceValueCallback,
              DefaultUpdateSourceTrigger = UpdateSourceTrigger.LostFocus
          }
      );
  
      private static object CoerceValueCallback(DependencyObject dependencyObject, object baseValue)
      {
          throw new NotImplementedException();
      }
  
      private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p><strong>View.xaml:</strong></p>
  
  <pre><code>&lt;UserControl x:Class="Launcher.Views.KnuthMorrisPrattView"
           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" 
           xmlns:views="clr-namespace:Launcher.Views"
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="500"
           DataContext="{Binding KnuthMorrisPrattViewModel, Source={StaticResource MainViewModel}}"&gt;
  &lt;Grid Margin="15"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="7*"/&gt;
          &lt;RowDefinition Height="3*"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;DockPanel Grid.Row="0"&gt;
          &lt;Label Content="Text" DockPanel.Dock="Top"&gt;&lt;/Label&gt;
          &lt;RichTextBox x:Name="TextBox" views:KnuthMorrisPrattView.Text="{Binding TextToSearchArg}"/&gt;
      &lt;/DockPanel&gt;
      &lt;DockPanel Grid.Row="1"&gt;
          &lt;Label Content="Pattern" DockPanel.Dock="Top"&gt;&lt;/Label&gt;
          &lt;TextBox Text="{Binding PatternArg}"/&gt;
      &lt;/DockPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>ViewModel.cs:</strong></p>
  
  <pre><code>using System.Windows.Controls;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using GalaSoft.MvvmLight.CommandWpf;
  using Launcher.Runners.KnuthMorrisPratt;
  
  namespace Launcher.ViewModels
  {
      public class KnuthMorrisPrattViewModel : ViewModelBase
      {
          private string _textToSearchArg;
          private string _patternArg;
  
          public string TextToSearchArg
          {
              get { return _textToSearchArg; }
              set
              {
                  _textToSearchArg = value;
                  RaisePropertyChanged();
              }
          }
  
          public string PatternArg
          {
              get { return _patternArg; }
              set
              {
                  _patternArg = value;
                  RaisePropertyChanged();
              }
          }           
  
          public KnuthMorrisPrattViewModel()
          {
  
          }            
      }
  }
  </code></pre>
  
  <p>I know that Callback throws and exception but my goal here is to just see under the debugger that this callback is invoked. Then I add correct implementation.</p>
  
  <p><strong>EDIT:</strong>
  I think I missed important note about my issue. When I update TextToSearchArg property from code everything works correctly. The only problem is that when I set some text in RichTextBox underlying property is not updated.</p>
  
  <p>What am I missing? Thanks a lot in advance.</p>