<?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; attached behavior</title>
	<atom:link href="http://www.japf.fr/tag/attached-behavior/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.japf.fr</link>
	<description>Jeremy Alles Presentation Foundation: WPF, .Net and modern software development</description>
	<lastBuildDate>Thu, 29 Jul 2010 07:29:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</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>
		<item>
		<title>Thinking in WPF: more attached properties</title>
		<link>http://www.japf.fr/2008/09/thinking-in-wpf-more-attached-properties/</link>
		<comments>http://www.japf.fr/2008/09/thinking-in-wpf-more-attached-properties/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 12:03:27 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[attached behavior]]></category>
		<category><![CDATA[attached properties]]></category>
		<category><![CDATA[textbox selectall]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=20</guid>
		<description><![CDATA[In my last blog post, I wrote an article about attached properties. Today at work, I encountered a problem that can be solved in a nice way using an attached property. Because the functionality I wanted to implement is also very simple, I decided to blog about it to give a concrete example. I wanted [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.japf.fr/?p=19">my last blog post</a>, I wrote an article about attached properties. Today at work, I encountered a problem that can be solved in a nice way using an attached property. Because the functionality I wanted to implement is also very simple, I decided to blog about it to give a concrete example.</p>
<p>I wanted to use a TextBox to allow the user of my application to give a description for any item he selects in a TreeView. Because the TextBox&#8217;s content could be changed very frequently by the user, I thought it might be useful to select all the TextBox&#8217;s text when the user click in the control (so that as soon as he types something, the old content is cleared).</p>
<p>In this article, I will describe various way to implement this feature and I will detail the way I prefer, using of course an attached property !</p>
<p><span id="more-20"></span></p>
<p><strong>Background</strong></p>
<p>With a TextBox control, a user can input text in an application. When the user click on a TextBox control, the control got focus and a cursor is displayed in the text.</p>
<p>The TextBox class has a <em>SelectAll()</em> method that can be used to select all the text in the control. In this example, what I want is to select all the text of the control when the user click on it. This allow very fast editing of the content without the need to delete the previous content or moving the cursor with the mouse.</p>
<p><strong>1. Using the code-behind file</strong></p>
<p>The most easy way is maybe to use the code-behind (for example mywindow.xaml.cs) and to subscribe to the events we want (here we must capture MouseDown, MouseUp and GotFocus events):</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><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('p20code9'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p209"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p20code9"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF; font-weight: bold;">public</span> Window1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            InitializeComponent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            textbox<span style="color: #008000;">.</span><span style="color: #0000FF;">AddHandler</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">GotFocusEvent</span>, <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>OnGotFocus<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            textbox<span style="color: #008000;">.</span><span style="color: #0000FF;">AddHandler</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">MouseDownEvent</span>, <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>OnGotFocus<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            textbox<span style="color: #008000;">.</span><span style="color: #0000FF;">AddHandler</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">MouseUpEvent</span>, <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>OnGotFocus<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnGotFocus<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sender <span style="color: #0600FF; font-weight: bold;">as</span> TextBox <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">&#41;</span>sender<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SelectAll</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This solution is not elegant because:</p>
<ul>
<li>it pollutes the code-behind file (that we should try to keep as clean as possible)</li>
<li>it is not reusable at all</li>
<li>we have to give a name to the control (how could we do that in a DataTemplate for example ?)</li>
</ul>
<p><strong>2. Using inheritance</strong></p>
<p>If you want to fix the 3 previous points, you can create a new control that inherit TextBox class and override MouseDown, MouseUp and GotFocus events:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><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('p20code10'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2010"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p20code10"><pre class="csharp" style="font-family:monospace;">    <span style="color: #6666cc; font-weight: bold;">class</span> TextboxInherited <span style="color: #008000;">:</span> TextBox
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnGotFocus<span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RoutedEventArgs</span> e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnGotFocus</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            SelectAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnMouseDown<span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Input</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MouseButtonEventArgs</span> e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnMouseDown</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            SelectAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnMouseUp<span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Input</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MouseButtonEventArgs</span> e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnMouseUp</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            SelectAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I don&#8217;t really like this solution neither because:</p>
<ul>
<li>it creates a new control only to add a very basic functionality</li>
<li>it is not possible to have a TextBox and enable the functionality &#8220;on the fly&#8221; (using a DataBinding for example)</li>
<li>it does not the leverage the attached property mechanism :p</li>
</ul>
<p><strong>3. Using an attached property</strong></p>
<p>The last solution might look more complicated for a simple feature, but it is also more powerful. I created a static class, SelectableTextbox that defines an attached property <em>SelectAllOnInput</em> of type bool. Setting this attached property to true on a Textbox will cause the OnSelectAllOnClickChanged fonction to add handler on the targeted Textbox.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><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('p20code11'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2011"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code" id="p20code11"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> SelectableTextBox
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080;">#region SelectAllOnClick attached property</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty SelectAllOnInputProperty <span style="color: #008000;">=</span>
            DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterAttached</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;SelectAllOnInput&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>SelectableTextBox<span style="color: #008000;">&#41;</span>,
                <span style="color: #008000;">new</span> FrameworkPropertyMetadata<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span><span style="color: #0600FF; font-weight: bold;">false</span>,
                    FrameworkPropertyMetadataOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">None</span>,
                    <span style="color: #008000;">new</span> PropertyChangedCallback<span style="color: #008000;">&#40;</span>OnSelectAllOnClickChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> GetSelectAllOnInput<span style="color: #008000;">&#40;</span>DependencyObject d<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>d<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>SelectAllOnInputProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetSelectAllOnInput<span style="color: #008000;">&#40;</span>DependencyObject d, <span style="color: #6666cc; font-weight: bold;">bool</span> value<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            d<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>SelectAllOnInputProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Handles changes to the SelectAllOnClick property.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnSelectAllOnClickChanged<span style="color: #008000;">&#40;</span>DependencyObject sender, DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sender <span style="color: #0600FF; font-weight: bold;">as</span> TextBox <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">NewValue</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">&#41;</span>sender<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddHandler</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">MouseUpEvent</span>, <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>OnSelectAllText<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">&#41;</span>sender<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddHandler</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">MouseDownEvent</span>, <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>OnSelectAllText<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">&#41;</span>sender<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddHandler</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">GotFocusEvent</span>, <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>OnSelectAllText<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sender <span style="color: #0600FF; font-weight: bold;">as</span> TextBox <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">NewValue</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">&#41;</span>sender<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RemoveHandler</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">MouseUpEvent</span>, <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>OnSelectAllText<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">&#41;</span>sender<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RemoveHandler</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">MouseDownEvent</span>, <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>OnSelectAllText<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">&#41;</span>sender<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RemoveHandler</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">.</span><span style="color: #0000FF;">GotFocusEvent</span>, <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>OnSelectAllText<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Handler that select all TextBox's text</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnSelectAllText<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>sender <span style="color: #0600FF; font-weight: bold;">as</span> TextBox <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>TextBox<span style="color: #008000;">&#41;</span>sender<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SelectAll</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In the XAML, it becomes very straightforward to enable the new functionality on any Textbox, we just need to set the SelectAllOnInput property to true:</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('p20code12'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2012"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p20code12"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">local:SelectableTextBox.SelectAllOnInput</span>=<span style="color: #ff0000;">&quot;True&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;test using an attached property&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p><strong>Conclusion</strong></p>
<p>In this article I showed 3 ways to add a basic functionality to the Textbox control. </p>
<p>The first one is maybe the one we&#8217;ll try first, to see if the behavior matched what we need. However, because it is not reusable and because it pollutes the code-behind file, I do not recommend it.</p>
<p>The second one is the traditional way to add a functionality to an existing control: using inheritance. This solutions works well but does not leverage the new WPF mechanisms. I also think it&#8217;s a shame to create a new control just to add this feature.</p>
<p>The latest solution is clean and reusable. Because it uses a very new way to think about the way we design our apps, it might be not the first one we think about. But once we get this attached behavior pattern into the head, it&#8217;s a great help <img src='http://www.japf.fr/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>Also, I recommend you to have a look at <a href="http://joshsmithonwpf.wordpress.com/2008/08/30/introduction-to-attached-behaviors/">Josh&#8217;s article</a> about attached behavior. Josh wrote an article about a very nice way to solve a problem that would be hard to fix without this feature.</p>
<p>Here is the <a href="http://www.japf.fr/download/japf_fr_TextboxSelectAllDemo.zip">demo project</a> that I used while writing this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/09/thinking-in-wpf-more-attached-properties/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
