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/
/raw/c5e16b513618c3dc26c78ff56c9c0be1/
c5e16b513618c3dc26c78ff56c9c0be1
csharp
C#
2
2019-06-26T15:15:54
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></span><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/
/raw/f9128398bb825bdff5981ba9ab631612/
f9128398bb825bdff5981ba9ab631612
xml
XML
6
2019-06-26T20:24:36
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

C++

Convert LPCWSTR to char string in VC++

1
2
3
4
5
6
7
#include <ole2.h> 
#define BUFFER_SIZE 100

size_t   i;
LPCWSTR strLPCWSTR = L"sometext";
char *strChar = new char[BUFFER_SIZE];
wcstombs_s(&i, strChar, (size_t)BUFFER_SIZE, strLPCWSTR, (size_t)BUFFER_SIZE);
https://snipt.net/embed/9b150d11d50e3dcc44ce471eb8058378/
/raw/9b150d11d50e3dcc44ce471eb8058378/
9b150d11d50e3dcc44ce471eb8058378
cpp
C++
8
2019-06-26T17:58:24
True
False
False
/api/public/snipt/50878/
convert-lpcwstr-to-char-string-in-vc
<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> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">#include</span> <span class="cpf">&lt;ole2.h&gt; </span><span class="cp"></span> </span><span id="L-2"><a name="L-2"></a><span class="cp">#define BUFFER_SIZE 100</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="kt">size_t</span> <span class="n">i</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a><span class="n">LPCWSTR</span> <span class="n">strLPCWSTR</span> <span class="o">=</span> <span class="s">L&quot;sometext&quot;</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a><span class="kt">char</span> <span class="o">*</span><span class="n">strChar</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">char</span><span class="p">[</span><span class="n">BUFFER_SIZE</span><span class="p">];</span> </span><span id="L-7"><a name="L-7"></a><span class="n">wcstombs_s</span><span class="p">(</span><span class="o">&amp;</span><span class="n">i</span><span class="p">,</span> <span class="n">strChar</span><span class="p">,</span> <span class="p">(</span><span class="kt">size_t</span><span class="p">)</span><span class="n">BUFFER_SIZE</span><span class="p">,</span> <span class="n">strLPCWSTR</span><span class="p">,</span> <span class="p">(</span><span class="kt">size_t</span><span class="p">)</span><span class="n">BUFFER_SIZE</span><span class="p">);</span> </span></pre></div> </td></tr></table>
C++

C++

C++ map insert

std::map<int,string> output;
output.insert(pair<int,string>(1,"hehehe"));
https://snipt.net/embed/6033040a2fa80a777ab2475084878b70/
/raw/6033040a2fa80a777ab2475084878b70/
6033040a2fa80a777ab2475084878b70
cpp
C++
2
2019-06-26T20:28:57
True
False
False
Aug 22, 2012 at 03:21 AM
/api/public/snipt/46099/
c-map-insert
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">std</span><span class="o">::</span><span class="n">map</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="n">string</span><span class="o">&gt;</span> <span class="n">output</span><span class="p">;</span> </span><span id="L-2"><a name="L-2"></a><span class="n">output</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">pair</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="n">string</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="s">&quot;hehehe&quot;</span><span class="p">));</span> </span></pre></div> </td></tr></table>
C++, map