snipt

Ctrl+h for KB shortcuts

C#

Pass parameter to RelayCommand in codebehind

ICommand command = new RelayCommand(x => this.Execute(parameterVariable), this.CanExecute);
https://snipt.net/embed/c5e16b513618c3dc26c78ff56c9c0be1/
https://snipt.net/raw/c5e16b513618c3dc26c78ff56c9c0be1/
c5e16b513618c3dc26c78ff56c9c0be1
csharp
C#
2
2016-09-27T08:11:37
True
False
False
Dec 06, 2013 at 12:50 PM
/api/public/snipt/98976/
pass-parameter-to-relaycommand-in-codebehind
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="n">ICommand</span> <span class="n">command</span> <span class="p">=</span> <span class="k">new</span> <span class="n">RelayCommand</span><span class="p">(</span><span class="n">x</span> <span class="p">=&gt;</span> <span class="k">this</span><span class="p">.</span><span class="n">Execute</span><span class="p">(</span><span class="n">parameterVariable</span><span class="p">),</span> <span class="k">this</span><span class="p">.</span><span class="n">CanExecute</span><span class="p">);</span> </span></pre></div> </td></tr></table>
RelayCommand, WPF

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://snipt.net/embed/f9128398bb825bdff5981ba9ab631612/
https://snipt.net/raw/f9128398bb825bdff5981ba9ab631612/
f9128398bb825bdff5981ba9ab631612
xml
XML
6
2016-09-27T08:13:55
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 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
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.