Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As per the answers in your <a href="https://stackoverflow.com/questions/7262137/what-is-datacontext-for">latest question</a>, the DataContext is the object that your Bindings will use as their data source.</p> <p>When I suggested setting the DataContext to the page or control (below, in original answer), this was so you didn't have to specify the <code>Source</code> property in your Binding as stated in your original question.</p> <p><strong>Example</strong> - Rather than specifying your Binding like:</p> <p><code>{Binding Path=PropertyName, RelativeSource={RelativeSource AncestorType={x:Type ns:ControlName}}}</code> </p> <p>you could have just used </p> <p><code>{Binding Path=PropertyName}</code></p> <p>as the containers <code>DataContext</code> property was set to the instance of <code>ControlName</code> (this is only the case if you specify <code>DataContext = this</code>, <em>not</em> <code>DataContext = this.DataContext</code> as that will just set it to itself and be pointless).</p> <p>Now for <code>Page</code> or <code>Window</code> objects, <code>PropertyName</code> can be either be a register <code>DependencyProperty</code> instance, or it can be a stock standard <code>Property</code> that raised the <code>INotifyPropertyChanged.PropertyChanged</code> event to notify the children in the XAML that the property has changed and they update themselves as required.</p> <p>For <code>Control</code> objects, you can still use either method - however, if you want to be able to bind to these properties, they will need to be defined as <code>DependencyProperty</code>s.</p> <p>Are you able to update your post with <em>any</em> source for us to look at for you?</p> <hr /> <p><strong>Original Answer:</strong></p> <p>I'm not entirely sure I understand your final goal, but the laziest way to specify a DataContext for your controls would be to set the page's DataContext property in the constructor to itself.</p> <p><code>this.DataContext = this;</code></p> <p>This eliminates the need to set the Source and RelativeSource properties in your Binding, while still allowing you to bind directly to the page properties.</p> <p>Example:</p> <pre><code>public class MyControl : UserControl { public static readonly DependencyProperty SomeStringProperty = DependencyProperty.Register("SomeString", typeof(string), typeof(ownerclass), new UIPropertyMetadata(0)); public string SomeString { get { return (string)GetValue(SomeStringProperty); } set { SetValue(SomeStringProperty, value); } } public MyControl() { InitializeComponent(); DataContext = this; } } </code></pre> <p>Then in your XAML:</p> <pre><code>&lt;TextBlock Text="{Binding Path=SomeString}" /&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload