snipt

Ctrl+h for KB shortcuts

XML

Binding to Parent Control's Property

1
2
3
4
5
6
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type view:myType}}, 
		 Path=DataContext.someProperty}

Parent Control's DataContext, most of time it is parent Control's ViewModel.

So we want to the child bind to the Property of the parent's ViewModel.
https://snippets.siftie.com/embed/f9128398bb825bdff5981ba9ab631612/
/raw/f9128398bb825bdff5981ba9ab631612/
f9128398bb825bdff5981ba9ab631612
xml
XML
6
2019-03-24T05:37:21
True
False
False
/api/public/snipt/89537/
binding-to-parent-controls-property
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type view:myType}}, </span><span id="L-2"><a name="L-2"></a> Path=DataContext.someProperty} </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a>Parent Control&#39;s DataContext, most of time it is parent Control&#39;s ViewModel. </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a>So we want to the child bind to the Property of the parent&#39;s ViewModel. </span></pre></div> </td></tr></table>
WPF