title: RelativeSource-Binding for UserControl
body: <p>I've created a <code>UserControl</code> for displaying a Hyperlink in my application. </p>
  
  <p>The markup of this <code>UserControl</code> looks like:</p>
  
  <pre><code>&lt;UserControl x:Class="MVVMExample.View.UserControls.ActionLink"
               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" d:DesignHeight="300" d:DesignWidth="300"
               DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt;
      &lt;Grid&gt;
          &lt;TextBlock Margin="5"&gt;
                  &lt;Hyperlink Command="{Binding LinkCommand}" CommandParameter="{Binding LinkCommandParameter}"&gt;
                      &lt;TextBlock Text="{Binding LinkText, UpdateSourceTrigger=PropertyChanged}"/&gt;
                  &lt;/Hyperlink&gt;
          &lt;/TextBlock&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>The <code>DataContext</code> of this <code>UserControl</code> is in the CodeBehind which looks like:</p>
  
  <pre><code>public partial class ActionLink : UserControl, INotifyPropertyChanged
  {
      public static readonly DependencyProperty LinkTextProperty = DependencyProperty.Register(
          "LinkText", typeof (string), typeof (ActionLink), new PropertyMetadata(LinkTextChanged));
  
      public static readonly DependencyProperty LinkCommandParameterProperty = DependencyProperty.Register(
          "LinkCommandParameter", typeof (object), typeof (ActionLink),
          new PropertyMetadata(LinkCommandParameterChanged));
  
      public static readonly DependencyProperty LinkCommandProperty = DependencyProperty.Register(
          "LinkCommand", typeof (ICommand), typeof (ActionLink), new PropertyMetadata(LinkCommandChanged));
  
      public ActionLink()
      {
          InitializeComponent();
      }
  
      public object LinkCommandParameter
      {
          get { return GetValue(LinkCommandParameterProperty); }
          set { SetValue(LinkCommandParameterProperty, value); }
      }
  
      public string LinkText
      {
          get { return (string) GetValue(LinkTextProperty); }
          set
          {
              SetValue(LinkTextProperty, value);
              OnPropertyChanged();
          }
      }
  
      public ICommand LinkCommand
      {
          get { return (ICommand) GetValue(LinkCommandProperty); }
          set { SetValue(LinkCommandProperty, value); }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      private static void LinkTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          ((ActionLink) d).LinkText = (string) e.NewValue;
      }
  
      private static void LinkCommandParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          ((ActionLink) d).LinkCommandParameter = e.NewValue;
      }
  
      private static void LinkCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          ((ActionLink) d).LinkCommand = (ICommand) e.NewValue;
      }
  
      protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  
  <p>Everything just works fine. </p>
  
  <p>Now if I want to use this <code>UserControl</code> with the Command-Binding I have to do the following:</p>
  
  <pre><code>&lt;userControls:ActionLink LinkText="View customers" LinkCommand="{Binding DataContext.ViewCustomersCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"/&gt;
  </code></pre>
  
  <p>If I use a <code>Button</code> instead I don't have to provide that <code>RelativeSource</code>. Is there a opportunity that I also don't have to provide a <code>RelativeSource</code> on binding properties of custom created <code>UserControl</code>s?</p>