title: Binding property to Silverlight dependency property independent of DataContext
body: <p>I'm trying to make an <code>Address</code> control that has an <code>IsReadOnly</code> property, which will make every <code>TextBox</code> inside read only when set to true.</p>
  
  <pre><code> &lt;my:AddressControl Grid.Column="1" Margin="5" IsReadOnly="True"/&gt;
  </code></pre>
  
  <p>I've managed to do this just fine with a dependency property and it works.</p>
  
  <p>Here's a simple class with the dependency property declared :</p>
  
  <pre><code>public partial class AddressControl : UserControl
  {
      public AddressControl()
      {
          InitializeComponent();
          this.DataContext = this;
      }
  
      public static readonly DependencyProperty IsReadOnlyProperty = 
         DependencyProperty.Register("IsReadOnly", typeof(bool), 
                                     typeof(AddressControl), null);
  
      public bool IsReadOnly
      {
          get { return (bool)GetValue(IsReadOnlyProperty); }
          set { SetValue(IsReadOnlyProperty, value); }
      }
  }
  </code></pre>
  
  <p>In the XAML for this codebehind file I have a <code>Textbox</code> for each address line:</p>
  
  <pre><code>  &lt;TextBox IsReadOnly="{Binding IsReadOnly}" Text="{Binding City, Mode=TwoWay}"/&gt;
    &lt;TextBox IsReadOnly="{Binding IsReadOnly}" Text="{Binding State, Mode=TwoWay}"/&gt;
    &lt;TextBox IsReadOnly="{Binding IsReadOnly}" Text="{Binding Zip, Mode=TwoWay}"/&gt;
  </code></pre>
  
  <p>Like i said this works just fine.
  The problem is that the <code>Address</code> control itself is bound to its parent object (I have several addresses I am binding).</p>
  
  <pre><code> &lt;my:AddressControl DataContext="{Binding ShippingAddress, Mode=TwoWay}" IsReadOnly="True"&gt;
   &lt;my:AddressControl DataContext="{Binding BillingAddress, Mode=TwoWay}" IsReadOnly="True"&gt;
  </code></pre>
  
  <p>The problem is that as soon as I set <code>DataContext</code> to something other than <code>'this'</code> then the binding for <code>IsReadOnly</code> breaks. Not surprising because its looking for <code>IsReadOnly</code> on the <code>Address</code> data entity and it doesn't exist or belong there.</p>
  
  <p>I've tried just about every combination of <a href="http://www.nbdtech.com/Free/WpfBinding.pdf" rel="nofollow noreferrer">binding attributes</a> to get <code>IsReadOnly</code> to bind to the  <code>AddressControl</code> obejct but can't get it working. </p>
  
  <p>I've tried things like this, but I can't get <code>IsReadOnly</code> to bind independently to the <code>AddressControl</code> property instead of its <code>DataContext</code>.</p>
  
  <pre><code>&lt;TextBox IsReadOnly="{Binding RelativeSource={RelativeSource Self}, Path=IsReadOnlyProperty}" Text="{Binding City, Mode=TwoWay}" /&gt;
  </code></pre>
  
  <p>I think I'm pretty close. What am I doing wrong?</p>