title: Bind a property of a selfmade user control to a property of the main-view-model
body: <p>I have a wpf application where I extended the RichTextBox to provide some specific functions. Lets call that new class BetterTextBox.</p>
  
  <p>Now, when I click in that TextBox TextBox.OnPreviewMouseLeftButtonUp is called and I am getting the CaretPosition:</p>
  
  <pre><code>    protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
      {
          PressedOffset = Document.ContentStart.GetOffsetToPosition(CaretPosition);      
      }
  
      public static readonly DependencyProperty PressedOffsetProperty 
            = DependencyProperty.Register("PressedOffset", typeof(int),
              typeof(ByteViewTextBox),
              new FrameworkPropertyMetadata()
          {
              DefaultValue = 0,
              BindsTwoWayByDefault = true,
              DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
          });
  
      public int PressedOffset
      {
          get { return (int)GetValue(PressedOffsetProperty); }
          set { SetValue(PressedOffsetProperty, value); Console.WriteLine("klöjgf");}
      }
  </code></pre>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Name="MainWindow"&gt;
      &lt;BetterTextBox /&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainWindow has MainViewModel as DataContext. What I want to do is, when a MouseClick in BetterTextBox occurs, a function in MainViewModel should be called. How can I get out of my UserControl a call a function in the MainViewModel?</p>
  
  <p>I tried something like this:</p>
  
  <pre><code>&lt;Window x:Name="MainWindow"&gt;
     &lt;BetterTextBox PressedOffset="{Binding ElementName=MainWindow, Path=MyFunction"/&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainViewModel:</p>
  
  <pre><code>public int MyFunction
  {
       set { callMyRealFunction(); }
  }
  </code></pre>
  
  <p>But that doesn't work. There is also a way to register a CallbackFunction for PressedOffsetProperty, but I am not sure how to register there a function from the MainViewModel which is not static.</p>