<?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 properties</title>
	<atom:link href="http://www.japf.fr/tag/attached-properties/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>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('p20code5'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p205"><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="p20code5"><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('p20code6'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p206"><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="p20code6"><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('p20code7'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p207"><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="p20code7"><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('p20code8'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p208"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p20code8"><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>4</slash:comments>
		</item>
		<item>
		<title>Thinking in WPF: attached properties</title>
		<link>http://www.japf.fr/2008/08/thinking-in-wpf-attached-properties/</link>
		<comments>http://www.japf.fr/2008/08/thinking-in-wpf-attached-properties/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 13:45:50 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[attached properties]]></category>
		<category><![CDATA[drag and drop]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=19</guid>
		<description><![CDATA[Today, I decided to start a series that I&#8217;m calling &#8220;Thinking in WPF&#8221;. As you already know, I do NOT consider myself as a WPF specialist, so those blog posts don&#8217;t aim at giving a perfect knowledge of the &#8220;best&#8221; (if there are any) ways of thinking in WPF. I&#8217;d just like to share what [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I decided to start a series that I&#8217;m calling &#8220;Thinking in WPF&#8221;. As you already know, I do <strong>NOT </strong>consider myself as a WPF specialist, so those blog posts don&#8217;t aim at giving a perfect knowledge of the &#8220;best&#8221; (if there are any) ways of thinking in WPF. I&#8217;d just like to share what I&#8217;m experiencing <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>In my previous post, I started to write about things that I think makes WPF different. In the current project I&#8217;m working on at work, I had a good example of a new way of thinking with WPF. Todays article deals with a new WPF concept called <em>Attached properties</em>.</p>
<p><span id="more-19"></span></p>
<p><strong>Example</strong></p>
<p>Imagine you need to display a list of person (that&#8217;s very original isn&#8217;t it ?). Each person has a name, a first name, and let&#8217;s say an address. Because you want to display a list of data, you&#8217;re probably going to use a ListView.You also would like to let the user sort this list of data in the order he wants, simply by dragging and dropping items in the control.</p>
<p>This is a feature that is possible to implement for a ListView (by handling common Drag&#8217;n'Drop events such as <em>DragEnter</em>, <em>DragOver</em>, <em>PreviewDrop</em>&#8230; Basically, you can think of two ways to implement this behavior:</p>
<ul>
<li>1. handle all Drag&#8217;n'Drop events in the code behind (C# file)</li>
<li>2. inherit ListView and add new features</li>
</ul>
<p>Well, in my opinion, none of those two options are very elegant:</p>
<ul>
<li>The first one pollutes the code behind file with &#8220;functional&#8221; code. I prefer trying to keep the code behind file as clean as possible. Moreover, this solution doesn&#8217;t allow ANY reuse.</li>
<li>The second one is maybe the first one we can think about, but&#8230; It forces to change all the ListView control to our NewListView control (which inherit ListView). I don&#8217;t like that solution that much.</li>
</ul>
<p><strong>What is an Attached property ?</strong></p>
<p>The good news is that WPF allows you to implement this by using an alternative, an <em>Attached property</em>. Let&#8217;s first have a look at the definition from MSDN website:</p>
<blockquote><p><em>&#8220;An attached property is a concept defined by Extensible Application Markup Language (XAML). An attached property is intended to be used as a type of global property that is settable on any object.&#8221;</em></p></blockquote>
<p>Hmmm&#8230; What does that mean ? Actually, if you already played with WPF, you already encountered a lot of attached properties. For example if you add an Image in a DockPanel, you might write something like</p>
<p>&lt;Image DockPanel.Dock=&#8221;Top&#8221; &#8230;/&gt;</p>
<p>Have you ever thought about the DockPanel.Dock part ? It is funny because Image objects don&#8217;t have a DockPanel property. Actually all UI element that you can dock (Image, TextBlock, anything) doesn&#8217;t have a DockPanel property, so how it is possible to write that ? It is possible because the DockPanel control defines an attached property.</p>
<p><strong>Attached properties as an extensibility mechanism: the Attached Behavior pattern</strong></p>
<p>As the name suggests, an attached property can be attached to arbitrary object. In the previous example (with DockPanel.Dock) we use an attached property to add an attribute to an object (we add the information &#8220;<em>I want to dock this image on the top</em>&#8220;). What is important, is that we can also use attached properties to provide extensibility without the need for traditional inheritance.</p>
<p>A very nice definition of the Attached Behavior can be found <a href="http://blogs.msdn.com/johngossman/archive/2008/05/07/the-attached-behavior-pattern.aspx">on the blog of John Gossman</a>:</p>
<blockquote><p>&#8220;The Attached Behavior pattern encapsulates &#8220;behavior&#8221; (usually user interactivity) into a class outside the visual heirarchy and allows it to be applied to a visual element by setting an attached property and hooking various events on the visual element.&#8221;</p></blockquote>
<p>How does that work ? Basically what we can do is the following:</p>
<ul>
<li>Declare a boolean attached property in a new class, for example DragDropHelper.cs</li>
<li>When the value of this attached property, it is possible to hook the interesting events (OnDrag, OnDrop&#8230;) and implement Drag&#8217;n'Drop operation in them</li>
</ul>
<p>I suggest you to have a look at <a href="http://www.beacosta.com/blog/?p=53">this excellent post</a> from Beatriz Costa which implement Drag&#8217;n'Drop using attached properties.</p>
<p>The benefits of this technique are the following:</p>
<ul>
<li>No need to change all your ListView control to a subclass that you would create by using standard inheritance principle</li>
<li>It becomes possible (as in <a href="http://www.beacosta.com/blog/?p=53">Beatriz Costa post</a>) to implement the Drag&#8217;n'Drop behaviour on the mode generic ItemsControl class</li>
</ul>
<p>There are plenty of things that can be achieved using attached properties. In this post, I talked about implementing Drag&#8217;n'Drop using an attached property, but a lot of things that could be done using inheritance can be done using attached properties.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/08/thinking-in-wpf-attached-properties/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

