<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JAPF &#187; Uncategorized</title>
	<atom:link href="http://www.japf.fr/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.japf.fr</link>
	<description>Jeremy Alles Presentation Foundation: WPF, Silverlight, Windows Phone 7, Windows 8</description>
	<lastBuildDate>Fri, 27 Jan 2012 07:57:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to attach commands to any UIElement ?</title>
		<link>http://www.japf.fr/2008/12/how-to-attach-commands-to-any-uielement/</link>
		<comments>http://www.japf.fr/2008/12/how-to-attach-commands-to-any-uielement/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 12:12:39 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[attached behavior]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[icommand]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=22</guid>
		<description><![CDATA[In this blog, I already wrote 2 posts about attached properties. The more I play with this new WPF concept, the more I like it. Today at work, I found one new nice use of attached properties, and because it is this time reusable, I decided to share my experience here. If you&#8217;re familiar with [...]]]></description>
			<content:encoded><![CDATA[<p>In this blog, I already wrote 2 posts about attached properties. The more I play with this new WPF concept, the more I like it. Today at work, I found one new nice use of attached properties, and because it is this time reusable, I decided to share my experience here.</p>
<p>If you&#8217;re familiar with WPF, then you&#8217;re probably familiar with Commands. Commands are a new concept in WPF, here is the introduction from <a href="http://msdn.microsoft.com/en-us/library/ms752308.aspx">MSDN</a> documentation:</p>
<p><em>Commanding is an input mechanism in Windows Presentation Foundation (WPF) which provides input handling at a more semantic level than device input. Examples of commands are the Copy, Cut, and Paste operations found on many applications. </em></p>
<p>Commands help you to decouple your UI from its execution logic and also simplify the process of enabling and disabling controls regarding the state of the command. If you want more details about Commands, you can check out <a href="http://marlongrech.wordpress.com/2007/11/11/wpf-commands/">this nice post</a> from <a href="http://marlongrech.wordpress.com">Marlon Grech</a>.</p>
<p>You can attach a command to a Button using its Command property (you might also use CommandParameter and CommandTarget properties in some cases). To be more precise, elements that support Command must implement the <em>ICommandSource </em>interface:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p22code3'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p223"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p22code3"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> ICommandSource
<span style="color: #008000;">&#123;</span>
ICommand Command <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #6666cc; font-weight: bold;">object</span> CommandParameter <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
IInputElement CommandTarget <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>If you open <a href="http://reflector.red-gate.com/">Reflector</a> and lookup for this interface, you&#8217;ll discover that 3 controls implement this interface:</p>
<ul>
<li>MenuItem</li>
<li>ButtonBase</li>
<li>Hyperlink</li>
</ul>
<p>In the project I&#8217;m working on at work, I had to find out a way to surpass 2 limitations:</p>
<ul>
<li>Defining commands on other controls than MenuItem, ButtonBase and Hyperlink</li>
<li>Defining commands that could be triggered on other event than MouseLeftButtonUp</li>
</ul>
<p>As you can imagine, I found a way to do that using&#8230; attached properties ! Basically I defined an attached property that I called MouseDoubleClickCommandProperty. This command enables you to attach a ICommand to <strong>ANY </strong>UIElement that will be triggered when the control is double clicked.</p>
<p>The MouseDoubleClickCommandProperty register a PropertyChangedCallback so that when the target changes, I can register on the UIElement.MouseDownEvent event. By looking the ClickCount property of the MouseButtonEventArgs parameter, I can check the MouseDownEvent comes from a DoubleClick event, and then trigger the associated command.</p>
<p>A nice example of this concept can be found in a TreeView. Imagine you want to start an action when a particular node in your TreeView is double clicked. The basic way to do that is to register the MouseDoubleClick event on the control. The new way to do the same thing is to use the MouseDoubleClickCommandProperty attached propery. Here is an example that shows how to do that in a hierarchical data template:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p22code4'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p224"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p22code4"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;HierarchicalDataTemplate</span></span>
<span style="color: #009900;">  <span style="color: #000066;">DataType</span>=<span style="color: #ff0000;">&quot;{x:Type treeViewModel:Node}&quot;</span> </span>
<span style="color: #009900;">  <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding Path=Children}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel</span> <span style="color: #000066;">Orientation</span>=<span style="color: #ff0000;">&quot;Horizontal&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">attached:ClickBehavior.MouseDoubleClickCommand</span>=<span style="color: #ff0000;">&quot;{Binding Path=Command}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Image</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;image&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;16&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;16&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;3,0&quot;</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;..\Resources\Icons\Treeview\Default16.png&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Name}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/HierarchicalDataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>As you can see, by using the MouseDoubleClickCommand attached property is becomes possible to attach the command to a StackPanel ! Moreoever, because we might need to pass parameter to the ICommand, I also defined another attached properties that can hold any parameter you want, this is the MouseEventParameterProperty. </p>
<p>Similarly, we can imagine to trigger Commands when a RightClick occurs (we would just have to define a new attached property to do so).</p>
<p>I did <a href="http://www.japf.fr/download/ClickCommandDemo.zip">a sample application</a> that demonstrates the concept of this article. Because I didn&#8217;t have too much time, I used a ListBox instead of a TreeView. but the concepts are equivalent. Please feel free to comment <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/12/how-to-attach-commands-to-any-uielement/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

