<?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>Veracity Solutions Blog &#187; Custom Controls</title>
	<atom:link href="http://blogs.veracitysolutions.com/category/custom-controls/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.veracitysolutions.com</link>
	<description>Software Consulting That Rocks</description>
	<lastBuildDate>Thu, 22 Apr 2010 17:21:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How To Animate a Changing Property in a Custom Control in Silverlight</title>
		<link>http://blogs.veracitysolutions.com/how-to-animate-a-changing-property-in-a-custom-control-in-silverlight/</link>
		<comments>http://blogs.veracitysolutions.com/how-to-animate-a-changing-property-in-a-custom-control-in-silverlight/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 16:10:44 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Custom Controls]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Templates]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Custom Control]]></category>
		<category><![CDATA[DependencyProperty]]></category>

		<guid isPermaLink="false">http://blogs.veracitysolutions.com/?p=661</guid>
		<description><![CDATA[For my recent visualization, I created a custom control in Silverlight that animated the color of a Path every time a “Fill” property on the control was changed. I thought I would pass along my learning from this process.
First thing I learned was that you cannot use TemplateBinding in a Storyboard (I think). I asked [...]]]></description>
			<content:encoded><![CDATA[<p>For my recent visualization, I created a custom control in Silverlight that animated the color of a Path every time a “Fill” property on the control was changed. I thought I would pass along my learning from this process.</p>
<p>First thing I learned was that you cannot use TemplateBinding in a Storyboard (I think). I asked this question on Stack Overflow and I haven’t gotten an answer. But I’m pretty sure that in Silverlight you cannot use TemplateBinding to attach a property to a KeyFrame value. This means that you have to have a pointer in the control code the allows access to the KeyFrame so you can update the value.</p>
<p>I’ll walk through the conceptual part of animating a property in a Custom Control in Sivlerlight and then walk through the code to it.</p>
<p><b>Step 1: Create a PART to your control to hold the Storyboard and a PART to hold the KeyFrame you want to manipulate</b></p>
<p>I cover <a href="http://blogs.veracitysolutions.com/how-to-create-a-part-in-your-silverlight-custom-control/">how to make a PART to your control here</a> because it was too much to put into a single blog post. You&#8217;re going to want to make your Storyboard a PART that is accessed via resources (via MyControlName.Resources[MyStoryboardName]) and your KeyFrame should be a PART that is accessed the normal way (via the GetTemplateChild(MyKeyFrameName) method). </p>
<p> <a href="http://11011.net/software/vspaste"></a>
<p><b>Step 2: Create the DependencyProperty you want to drive the animation</b></p>
<p>Yet another chance for me to endlessly flog <a href="http://blog.nerdplusart.com/archives/silverlight-code-snippets">Robby Ingebretsen’s awesome Silverlight Code Snippets</a>. If you use these, use ‘sldpc’, which is the DependencyProperty with a property changed callback. We want to manually fire the animation when the property changes, so we’re going to do that in the callback.</p>
<p>If you don’t want to use Robby’s stuff, here’s the code for a DependencyProperty with a property changed callback. Because my visualization animates the color, I called my property “Fill”. </p>
<p><span style="color: blue">public </span><span style="color: #2b91af">SolidColorBrush </span>Fill    <br />{    <br />&#160;&#160;&#160; <span style="color: blue">get </span>{ <span style="color: blue">return </span>(<span style="color: #2b91af">SolidColorBrush</span>)GetValue(FillProperty); }    <br />&#160;&#160;&#160; <span style="color: blue">set </span>{ SetValue(FillProperty, <span style="color: blue">value</span>); }    <br />}    </p>
<p><span style="color: blue">public static readonly </span><span style="color: #2b91af">DependencyProperty </span>FillProperty =    <br />&#160;&#160;&#160; <span style="color: #2b91af">DependencyProperty</span>.Register(<span style="color: #a31515">&quot;Fill&quot;</span>, <span style="color: blue">typeof</span>(<span style="color: #2b91af">SolidColorBrush</span>), <span style="color: blue">typeof</span>(<span style="color: #2b91af">County</span>),     <br />&#160;&#160;&#160; <span style="color: blue">new </span><span style="color: #2b91af">PropertyMetadata</span>(<span style="color: blue">new </span><span style="color: #2b91af">SolidColorBrush</span>(<span style="color: #2b91af">Color</span>.FromArgb(0, 0, 0, 0)), <span style="color: blue">new </span><span style="color: #2b91af">PropertyChangedCallback</span>(OnFillChanged)));</p>
<p> <a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a>
<p><b>Step 3: Hook up a property changed callback and run the animation</b></p>
<p>Then, all you need to do is go into the property changed callback that you have created and assign your the new valie of the property to the keyframe value of the animation you want to run. Then run the animation.</p>
<p><span style="color: blue">private static void </span>OnFillChanged(<span style="color: #2b91af">DependencyObject </span>d, <span style="color: #2b91af">DependencyPropertyChangedEventArgs </span>e)    <br />{    <br />&#160;&#160;&#160; <span style="color: #2b91af">MyControlType </span>thisControl = d <span style="color: blue">as </span><span style="color: #2b91af">MyControlType</span>;    <br />&#160;&#160;&#160; <span style="color: blue">if </span>((thisControl != <span style="color: blue">null</span>) &amp;&amp; (thisControl._myStoryboard != <span style="color: blue">null</span>))    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">SolidColorBrush </span>newBrushHolder = (<span style="color: #2b91af">SolidColorBrush</span>)e.NewValue;    </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; thisCounty._myKeyFrame.Value = newBrushHolder.Color;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; thisCounty._myStoryboard.Begin();    <br />&#160;&#160;&#160; }    <br />}</p>
<p> <a href="http://11011.net/software/vspaste"></a>
<p>And now every time you change that value in the control, it will animate to the new value. Simple as that. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/how-to-animate-a-changing-property-in-a-custom-control-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How To Create a PART in Your Silverlight Custom Control</title>
		<link>http://blogs.veracitysolutions.com/how-to-create-a-part-in-your-silverlight-custom-control/</link>
		<comments>http://blogs.veracitysolutions.com/how-to-create-a-part-in-your-silverlight-custom-control/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 16:06:22 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Custom Controls]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[Custom Control]]></category>
		<category><![CDATA[PART]]></category>

		<guid isPermaLink="false">http://blogs.veracitysolutions.com/?p=656</guid>
		<description><![CDATA[There are about a dozen videos and tutorials on how to create a custom control in Silverlight 3. (Here’s my favorite, from Karen Corby. Skip to about 47 minutes for the tutorial part.)
But sometimes, you (and by “you”, I mean me) need help figuring out how to create a PART for a control because, let’s [...]]]></description>
			<content:encoded><![CDATA[<p>There are about a dozen videos and tutorials on how to create a custom control in Silverlight 3. (<a href="http://scorbs.com/2009/03/23/mix-09-building-microsoft-silverlight-controls/">Here’s my favorite, from Karen Corby</a>. Skip to about 47 minutes for the tutorial part.)</p>
<p>But sometimes, you (and by “you”, I mean me) need help figuring out how to create a PART for a control because, let’s be honest, you don’t do it all the time and no one has a blog post on just this little part of control building.</p>
<p>If you’re wondering what a PART is, here’s a short explanation: A PART is a naming convention that allows a control (code) to make a contract with the control template (XAML) in Silverlight. The control says to the template, “I’m going to be doing something programmatic with this part, so it better be there”. The template responds, “Here’s that part you wanted.”</p>
<p>I not actually talking down to anyone, this is really how my brain works. </p>
<p>There are basically five steps in creating a PART. I think you can actually do it in less, but I’m trying to follow what I think is proper coding practice. In this tutorial, we’re going create parts out of a Button and also a Storyboard (partly because there are different ways to assign a PART if it is in located in the control resources like a Storyboard is).</p>
<p><b>Step 1: Define the control template parts you’ll need</b></p>
<p>Outside of the control class we need to add the TemplatePart attributes. Inside the class, we need to define the names of the UI objects we’re going to turn into parts as well as create some corresponding objects in the control so we can access those PARTs.</p>
<p>[<span style="color: #2b91af">TemplatePart</span>(Name = <span style="color: #2b91af">MyControl</span>.StoryboardString, Type = <span style="color: blue">typeof</span>(<span style="color: #2b91af">Storyboard</span>))]    <br />[<span style="color: #2b91af">TemplatePart</span>(Name = <span style="color: #2b91af">MyControl</span>.RootElementString, Type = <span style="color: blue">typeof</span>(<span style="color: #2b91af">FrameworkElement</span>))]    <br />[<span style="color: #2b91af">TemplatePart</span>(Name = <span style="color: #2b91af">MyControl</span>.ButtonString, Type = <span style="color: blue">typeof</span>(<span style="color: #2b91af">Button</span>))]    </p>
<p><span style="color: blue">public class </span><span style="color: #2b91af">MyControl </span>: <span style="color: #2b91af">Control     <br /></span>{    <br />&#160;&#160;&#160; <span style="color: blue">private const string </span>StoryboardString = <span style="color: #a31515">&quot;PART_MyStoryboard&quot;</span>;    <br />&#160;&#160;&#160; <span style="color: blue">private const string </span>RootElementString = <span style="color: #a31515">&quot;PART_RootElement&quot;</span>;&#160; <br />&#160;&#160;&#160; <span style="color: blue">private const string </span>ButtonString = <span style="color: #a31515">&quot;PART_MyButton&quot;</span>;</p>
<p>&#160;&#160;&#160; <span style="color: blue">private </span><span style="color: #2b91af">Storyboard </span>_targetStoryboard;    <br /><span style="color: blue">&#160;&#160;&#160; private </span><span style="color: #2b91af">FrameworkElement </span>_rootElement;    <br /><span style="color: blue">&#160;&#160;&#160; private </span><span style="color: #2b91af">Button </span>_targetButton;<a href="http://11011.net/software/vspaste"></a></p>
<p>What we’re going to do here is remarkably stupid because I don’t have the creativity right now to do something cool. When we press the button, we’ll play the storyboard. We need the RootElement because… you’ll see in a moment. </p>
<p><b>Step 2: Make sure you have a XAML template properly named to correspond with the part</b></p>
<p>We have three parts we’re getting here, so this is a stripped down template that just has the three PARTs named appropriately. If the names are not the same as the strings above, it will not work.</p>
<p><span style="color: blue">&lt;</span><span style="color: #a31515">Style</span><span style="color: red">TargetType</span><span style="color: blue">=&quot;local:MyControl&quot;&gt;     <br />&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Setter</span><span style="color: red">Property</span><span style="color: blue">=&quot;Template&quot;&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">ControlTemplate </span><span style="color: red">TargetType</span><span style="color: blue">=&quot;local:MyControl&quot;&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Grid </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Name</span><span style="color: blue">=&quot;PART_RootElement&quot;&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Grid.Resources</span><span style="color: blue">&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Storyboard </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Name</span><span style="color: blue">=&quot;PART_MyStoryboard&quot;&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: green">&lt;!&#8211; Whatever animations you want here &#8211;&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">&lt;/</span><span style="color: #a31515">Storyboard</span><span style="color: blue">&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Grid.Resources</span><span style="color: blue">&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Button </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Name</span><span style="color: blue">=&quot;PART_MyButton&quot; /&gt;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Grid</span><span style="color: blue">&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">ControlTemplate</span><span style="color: blue">&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Setter.Value</span><span style="color: blue">&gt;     <br />&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Setter</span><span style="color: blue">&gt;     <br />&lt;/</span><span style="color: #a31515">Style</span><span style="color: blue">&gt;</span></p>
<p> <a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a>
<p><b>Step 3: Assign the XAML PART to the code PART in the OnApplyTemplate method</b></p>
<p>Now we just have to write a custom OnApplyTemplate method that will grab the XAML elements and assign them to objejcts within the control code. There are two key methods you’ll use. The first is GetTemplateChild, which will pull out any UI element in the XAML that is named and not in the resources. </p>
<p>If you want to get something in the resources, you need to refer to the UI element that holds those resources. This is why we needed the root element as a PART. So, enough of my blabbing… here’s the code:</p>
<p><span style="color: blue">public override void </span>OnApplyTemplate()    <br />{    <br />&#160;&#160;&#160; <span style="color: blue">base</span>.OnApplyTemplate();    </p>
<p>&#160;&#160;&#160; _rootElement = GetTemplateChild(<span style="color: #2b91af">MyControl</span>.RootElementString) <span style="color: blue">as </span><span style="color: #2b91af">FrameworkElement</span>;    <br />&#160;&#160;&#160; <span style="color: green">//Make sure we actually got the template applied.     <br />&#160;&#160;&#160; </span><span style="color: blue">if </span>(_rootElement != <span style="color: blue">null</span>)    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; _targetStoryboard = _rootElement.Resources[<span style="color: #2b91af">MyControl</span>.StoryboardString] <span style="color: blue">as </span><span style="color: #2b91af">Storyboard</span>;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; _targetButton = GetTemplateChild(<span style="color: #2b91af">MyControl</span>.ButtonString) <span style="color: blue">as </span><span style="color: #2b91af">Button</span>;</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; _targetButton.Click +=<span style="color: blue">new </span><span style="color: #2b91af">RoutedEventHandler</span>(_targetButton_Click);&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p>
<p>&#160;&#160;&#160; }    <br />}</p>
<p> <a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a>
<p>Now we have all the PARTs assigned so we can do something like adding an event handler to the Button so that, when it is clicked, it runs the storyboard.</p>
<p><span style="color: blue">void&#160; </span>_targetButton_Click(<span style="color: blue">object</span>sender, <span style="color: #2b91af">RoutedEventArgs </span>e)    <br />{    <br />&#160;&#160;&#160;&#160; _targetStoryboard.Begin();    <br />}</p>
<p> <a href="http://11011.net/software/vspaste"></a>
<p>And that’s how you add a PART… both in the regular XAML template and in the resources of the template.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/how-to-create-a-part-in-your-silverlight-custom-control/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Building Custom Template-able WPF Controls</title>
		<link>http://blogs.veracitysolutions.com/building-custom-template-able-wpf-controls/</link>
		<comments>http://blogs.veracitysolutions.com/building-custom-template-able-wpf-controls/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 04:08:40 +0000</pubDate>
		<dc:creator>Joe McBride</dc:creator>
				<category><![CDATA[Custom Controls]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">f86693b4-c59e-43d4-8c4a-670d55e24c19:811</guid>
		<description><![CDATA[Building custom controls in WPF can provide you with lots of flexibility.  It allows you to entirely separate the behavior of the control from the look of the control.  This is the premise behind most of what WPF offers.  In this post I will show you how you can build a simple control similar to [...]]]></description>
			<content:encoded><![CDATA[<p>Building custom controls in WPF can provide you with lots of flexibility.  It allows you to entirely separate the behavior of the control from the look of the control.  This is the premise behind most of what WPF offers.  In this post I will show you how you can build a simple control similar to the search control in Outlook 2007.</p>
<p><a href="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/FilterTextBoxDemo3.png"><img src="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/FilterTextBoxDemo3_thumb.png" border="0" alt="Filter TextBox" width="310" height="141" /></a></p>
<p>Add a new WPF Application project.</p>
<p><a href="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/NewProject.png"><img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" src="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/NewProject_thumb.png" border="0" alt="New Project" width="640" height="392" /></a></p>
<p>Then add a WPF User Control Library.</p>
<p><a href="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/AddNewProject.png"><img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" src="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/AddNewProject_thumb.png" border="0" alt="Add New Project" width="640" height="416" /></a></p>
<p>Delete the generated UserControl1.xaml that was given to you.</p>
<p><a href="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/MicrosoftVisualStudio.png"><img src="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/MicrosoftVisualStudio_thumb.png" border="0" alt="Microsoft Visual Studio" width="407" height="201" /></a></p>
<p>Add a new WPF Custom Control.</p>
<p><a href="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/AddNewItemFilterTextBox2.png"><img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" src="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/AddNewItemFilterTextBox2_thumb.png" border="0" alt="Add New Item - FilterTextBox" width="640" height="393" /></a></p>
<p>Your solution should now look like this:</p>
<p><a href="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/WindowClipping17.png"><img src="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/WindowClipping17_thumb.png" border="0" alt="Solution Explorer" width="286" height="288" /></a></p>
<p>The template gives you a FilterTextBox.cs and Generic.xaml file.</p>
<div style="border-right:white 2px ridge;padding-right:10px;border-top:white 2px ridge;padding-left:10px;font-size:10pt;background:#eeeeee;padding-bottom:10px;margin:10px;border-left:white 2px ridge;color:black;padding-top:10px;border-bottom:white 2px ridge;font-family:consolas, courier new;word-wrap:break-word;">
<pre style="margin:0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">FilterTextBox</span> : <span style="color: #2b91af;">Control</span></pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: blue;">static</span> FilterTextBox()</pre>
<pre style="margin:0px;">    {</pre>
<pre style="margin:0px;">        DefaultStyleKeyProperty.OverrideMetadata(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">FilterTextBox</span>),</pre>
<pre style="margin:0px;">            <span style="color: blue;">new</span> <span style="color: #2b91af;">FrameworkPropertyMetadata</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">FilterTextBox</span>)));</pre>
<pre style="margin:0px;">    }</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>The default Generic.xaml is the default look for your custom control, and is found in the Theme directory.  It is just an empty border for now:</p>
<div style="border-right:white 2px ridge;padding-right:10px;border-top:white 2px ridge;padding-left:10px;font-size:10pt;background:#eeeeee;padding-bottom:10px;margin:10px;border-left:white 2px ridge;color:black;padding-top:10px;border-bottom:white 2px ridge;font-family:consolas, courier new;word-wrap:break-word;">
<pre style="margin:0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">Style</span><span style="color: blue;"> </span><span style="color: red;">TargetType</span><span style="color: blue;">=</span>"<span style="color: blue;">{x:Type local:FilterTextBox}</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;</span><span style="color: #a31515;">Setter</span><span style="color: blue;"> </span><span style="color: red;">Property</span><span style="color: blue;">=</span>"<span style="color: blue;">Template</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;</span><span style="color: #a31515;">Setter.Value</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      &lt;</span><span style="color: #a31515;">ControlTemplate</span><span style="color: blue;"> </span><span style="color: red;">TargetType</span><span style="color: blue;">=</span>"<span style="color: blue;">{x:Type local:FilterTextBox}</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">        &lt;</span><span style="color: #a31515;">Border</span><span style="color: blue;"> </span><span style="color: red;">Background</span><span style="color: blue;">=</span>"<span style="color: blue;">{TemplateBinding Background}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">                </span><span style="color: red;">BorderBrush</span><span style="color: blue;">=</span>"<span style="color: blue;">{TemplateBinding BorderBrush}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">                </span><span style="color: red;">BorderThickness</span><span style="color: blue;">=</span>"<span style="color: blue;">{TemplateBinding BorderThickness}</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">        &lt;/</span><span style="color: #a31515;">Border</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      &lt;/</span><span style="color: #a31515;">ControlTemplate</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;/</span><span style="color: #a31515;">Setter.Value</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;/</span><span style="color: #a31515;">Setter</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">Style</span><span style="color: blue;">&gt;</span></pre>
</div>
<p>Now lets start building the behavior of our control.  We&#8217;ll start by adding a &#8216;Text&#8217; dependency property.  This will be the filter text that the user types in.  Notice that I&#8217;ve created callbacks to be notified when the property has changed.  And as always, do NOT put any code within the getter and setter of the CLR property, because WPF bypasses this property at runtime and calls GetValue and SetValue directly.  However the CLR property is still needed to use the property in xaml.</p>
<div style="border-right:white 2px ridge;padding-right:10px;border-top:white 2px ridge;padding-left:10px;font-size:10pt;background:#eeeeee;padding-bottom:10px;margin:10px;border-left:white 2px ridge;color:black;padding-top:10px;border-bottom:white 2px ridge;font-family:consolas, courier new;word-wrap:break-word;">
<pre style="margin:0px;"><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">DependencyProperty</span> TextProperty =</pre>
<pre style="margin:0px;">    <span style="color: #2b91af;">DependencyProperty</span>.Register(<span style="color: #a31515;">"Text"</span>,</pre>
<pre style="margin:0px;">                                <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">String</span>),</pre>
<pre style="margin:0px;">                                <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">FilterTextBox</span>),</pre>
<pre style="margin:0px;">                                <span style="color: blue;">new</span> <span style="color: #2b91af;">UIPropertyMetadata</span>(<span style="color: blue;">null</span>,</pre>
<pre style="margin:0px;">                                    <span style="color: blue;">new</span> <span style="color: #2b91af;">PropertyChangedCallback</span>(OnTextChanged),</pre>
<pre style="margin:0px;">                                    <span style="color: blue;">new</span> <span style="color: #2b91af;">CoerceValueCallback</span>(OnCoerceText))</pre>
<pre style="margin:0px;">                               );</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;"><span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">object</span> OnCoerceText(<span style="color: #2b91af;">DependencyObject</span> o, <span style="color: #2b91af;">Object</span> value)</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: #2b91af;">FilterTextBox</span> filterTextBox = o <span style="color: blue;">as</span> <span style="color: #2b91af;">FilterTextBox</span>;</pre>
<pre style="margin:0px;">    <span style="color: blue;">if</span> (filterTextBox != <span style="color: blue;">null</span>)</pre>
<pre style="margin:0px;">        <span style="color: blue;">return</span> filterTextBox.OnCoerceText((<span style="color: #2b91af;">String</span>)value);</pre>
<pre style="margin:0px;">    <span style="color: blue;">else</span></pre>
<pre style="margin:0px;">        <span style="color: blue;">return</span> value;</pre>
<pre style="margin:0px;">}</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;"><span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> OnTextChanged(<span style="color: #2b91af;">DependencyObject</span> o,</pre>
<pre style="margin:0px;">                                  <span style="color: #2b91af;">DependencyPropertyChangedEventArgs</span> e)</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: #2b91af;">FilterTextBox</span> filterTextBox = o <span style="color: blue;">as</span> <span style="color: #2b91af;">FilterTextBox</span>;</pre>
<pre style="margin:0px;">    <span style="color: blue;">if</span> (filterTextBox != <span style="color: blue;">null</span>)</pre>
<pre style="margin:0px;">        filterTextBox.OnTextChanged((<span style="color: #2b91af;">String</span>)e.OldValue, (<span style="color: #2b91af;">String</span>)e.NewValue);</pre>
<pre style="margin:0px;">}</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;"><span style="color: blue;">protected</span> <span style="color: blue;">virtual</span> <span style="color: #2b91af;">String</span> OnCoerceText(<span style="color: #2b91af;">String</span> value)</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: blue;">return</span> value;</pre>
<pre style="margin:0px;">}</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;"><span style="color: blue;">protected</span> <span style="color: blue;">virtual</span> <span style="color: blue;">void</span> OnTextChanged(<span style="color: #2b91af;">String</span> oldValue, <span style="color: #2b91af;">String</span> newValue)</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">}</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;"><span style="color: blue;">public</span> <span style="color: #2b91af;">String</span> Text</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: green;">// IMPORTANT: To maintain parity between setting a property in XAML</span></pre>
<pre style="margin:0px;">    <span style="color: green;">// and procedural code, do not touch the getter and setter inside</span></pre>
<pre style="margin:0px;">    <span style="color: green;">// this dependency property!</span></pre>
<pre style="margin:0px;">    <span style="color: blue;">get</span></pre>
<pre style="margin:0px;">    {</pre>
<pre style="margin:0px;">        <span style="color: blue;">return</span> (<span style="color: #2b91af;">String</span>)GetValue(TextProperty);</pre>
<pre style="margin:0px;">    }</pre>
<pre style="margin:0px;">    <span style="color: blue;">set</span></pre>
<pre style="margin:0px;">    {</pre>
<pre style="margin:0px;">        SetValue(TextProperty, <span style="color: blue;">value</span>);</pre>
<pre style="margin:0px;">    }</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>We&#8217;ll want users of the control to be notified when the text in our TextBox has changed, so lets create a &#8216;TextChanged&#8217; event.</p>
<div style="border-right:white 2px ridge;padding-right:10px;border-top:white 2px ridge;padding-left:10px;font-size:10pt;background:#eeeeee;padding-bottom:10px;margin:10px;border-left:white 2px ridge;color:black;padding-top:10px;border-bottom:white 2px ridge;font-family:consolas, courier new;word-wrap:break-word;">
<pre style="margin:0px;"><span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">RoutedEvent</span> TextChangedEvent =</pre>
<pre style="margin:0px;">    <span style="color: #2b91af;">EventManager</span>.RegisterRoutedEvent(<span style="color: #a31515;">"TextChanged"</span>,</pre>
<pre style="margin:0px;">                                    <span style="color: #2b91af;">RoutingStrategy</span>.Bubble,</pre>
<pre style="margin:0px;">                                    <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">RoutedEventHandler</span>),</pre>
<pre style="margin:0px;">                                    <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">FilterTextBox</span>));</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;"><span style="color: blue;">public</span> <span style="color: blue;">event</span> <span style="color: #2b91af;">RoutedEventHandler</span> TextChanged</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: blue;">add</span> { AddHandler(TextChangedEvent, <span style="color: blue;">value</span>); }</pre>
<pre style="margin:0px;">    <span style="color: blue;">remove</span> { RemoveHandler(TextChangedEvent, <span style="color: blue;">value</span>); }</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>Now lets go back and modify our OnTextChanged method to raise our TextChanged event.</p>
<div style="border-right:white 2px ridge;padding-right:10px;border-top:white 2px ridge;padding-left:10px;font-size:10pt;background:#eeeeee;padding-bottom:10px;margin:10px;border-left:white 2px ridge;color:black;padding-top:10px;border-bottom:white 2px ridge;font-family:consolas, courier new;word-wrap:break-word;">
<pre style="margin:0px;"><span style="color: blue;">protected</span> <span style="color: blue;">virtual</span> <span style="color: blue;">void</span> OnTextChanged(<span style="color: #2b91af;">String</span> oldValue, <span style="color: #2b91af;">String</span> newValue)</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: green;">// fire text changed event</span></pre>
<pre style="margin:0px;">    <span style="color: blue;">this</span>.RaiseEvent(<span style="color: blue;">new</span> <span style="color: #2b91af;">RoutedEventArgs</span>(<span style="color: #2b91af;">FilterTextBox</span>.TextChangedEvent, <span style="color: blue;">this</span>));</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>With the base behavior mostly done, we can move on to creating a generic look for our control.  Lets add a DockPanel with a Button and a TextBox, the Button docked to the right.  Lets also bind the &#8216;Text&#8217; property of the TextBox to the &#8216;Text&#8217; property of our control.  We want the &#8216;UpdateSourceTrigger&#8217; to be &#8216;PropertyChanged&#8217; so that the &#8216;TextChanged&#8217; event we created will be fired every time the user types something into the TextBox.  Notice that we don&#8217;t want the TextBox to have a border because then we&#8217;d have two borders.</p>
<div style="border-right:white 2px ridge;padding-right:10px;border-top:white 2px ridge;padding-left:10px;font-size:10pt;background:#eeeeee;padding-bottom:10px;margin:10px;border-left:white 2px ridge;color:black;padding-top:10px;border-bottom:white 2px ridge;font-family:consolas, courier new;word-wrap:break-word;">
<pre style="margin:0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">ControlTemplate</span><span style="color: blue;"> </span><span style="color: red;">TargetType</span><span style="color: blue;">=</span>"<span style="color: blue;">{x:Type local:FilterTextBox}</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;</span><span style="color: #a31515;">Border</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">Background</span><span style="color: blue;">=</span>"<span style="color: blue;">{TemplateBinding Background}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">BorderBrush</span><span style="color: blue;">=</span>"<span style="color: blue;">{TemplateBinding BorderBrush}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">BorderThickness</span><span style="color: blue;">=</span>"<span style="color: blue;">{TemplateBinding BorderThickness}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">CornerRadius</span><span style="color: blue;">=</span>"<span style="color: blue;">3</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;</span><span style="color: #a31515;">DockPanel</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">LastChildFill</span><span style="color: blue;">=</span>"<span style="color: blue;">True</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">Margin</span><span style="color: blue;">=</span>"<span style="color: blue;">1</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      &lt;</span><span style="color: #a31515;">Button</span></pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">x:Name</span><span style="color: blue;">=</span>"<span style="color: blue;">PART_ClearFilterButton</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">Content</span><span style="color: blue;">=</span>"<span style="color: blue;">X</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">Width</span><span style="color: blue;">=</span>"<span style="color: blue;">20</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">ToolTip</span><span style="color: blue;">=</span>"<span style="color: blue;">Clear Filter</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">DockPanel.Dock</span><span style="color: blue;">=</span>"<span style="color: blue;">Right</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      &lt;</span><span style="color: #a31515;">TextBox</span></pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">x:Name</span><span style="color: blue;">=</span>"<span style="color: blue;">PART_FilterTextBox</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">Text</span><span style="color: blue;">=</span>"<span style="color: blue;">{Binding Path=Text,</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                      Mode=TwoWay,</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                      UpdateSourceTrigger=PropertyChanged,</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                      RelativeSource={RelativeSource TemplatedParent}}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">BorderBrush</span><span style="color: blue;">=</span>"<span style="color: blue;">{x:Null}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">BorderThickness</span><span style="color: blue;">=</span>"<span style="color: blue;">0</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">VerticalAlignment</span><span style="color: blue;">=</span>"<span style="color: blue;">Center</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;/</span><span style="color: #a31515;">DockPanel</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;/</span><span style="color: #a31515;">Border</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">ControlTemplate</span><span style="color: blue;">&gt;</span></pre>
</div>
<p>Take special note of the names of the controls.  They both begin with &#8216;PART_&#8217;.  This is the standard way in WPF to signify controls that need to be replaced if you decide to change the template of the control.  If someone templates your control, you need some way to identify that the types used for your parts need to be ones that your control can use.  You can do this by using the TemplatePart attribute and giving it the name and type of your control parts.</p>
<div style="border-right:white 2px ridge;padding-right:10px;border-top:white 2px ridge;padding-left:10px;font-size:10pt;background:#eeeeee;padding-bottom:10px;margin:10px;border-left:white 2px ridge;color:black;padding-top:10px;border-bottom:white 2px ridge;font-family:consolas, courier new;word-wrap:break-word;">
<pre style="margin:0px;">[<span style="color: #2b91af;">TemplatePart</span>(Name = <span style="color: #a31515;">"PART_FilterTextBox"</span>, Type = <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">TextBox</span>))]</pre>
<pre style="margin:0px;">[<span style="color: #2b91af;">TemplatePart</span>(Name = <span style="color: #a31515;">"PART_ClearFilterButton"</span>, Type = <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Button</span>))]</pre>
<pre style="margin:0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">FilterTextBox</span> : <span style="color: #2b91af;">Control</span></pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    ...</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>We only want the &#8216;Clear Filter&#8217; button to be showing when there is text in the TextBox, so lets create a DataTrigger to accomplish that for us.</p>
<div style="border-right:white 2px ridge;padding-right:10px;border-top:white 2px ridge;padding-left:10px;font-size:10pt;background:#eeeeee;padding-bottom:10px;margin:10px;border-left:white 2px ridge;color:black;padding-top:10px;border-bottom:white 2px ridge;font-family:consolas, courier new;word-wrap:break-word;">
<pre style="margin:0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">ControlTemplate</span><span style="color: blue;"> </span><span style="color: red;">TargetType</span><span style="color: blue;">=</span>"<span style="color: blue;">{x:Type local:FilterTextBox}</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;</span><span style="color: #a31515;">Border</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;">    ...</pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;/</span><span style="color: #a31515;">Border</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;</span><span style="color: #a31515;">ControlTemplate.Triggers</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;</span><span style="color: #a31515;">DataTrigger</span><span style="color: blue;"> </span><span style="color: red;">Binding</span><span style="color: blue;">=</span>"<span style="color: blue;">{Binding Path=Text.Length, ElementName=PART_FilterTextBox}</span>"<span style="color: blue;"> </span><span style="color: red;">Value</span><span style="color: blue;">=</span>"<span style="color: blue;">0</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      &lt;</span><span style="color: #a31515;">Setter</span><span style="color: blue;"> </span><span style="color: red;">TargetName</span><span style="color: blue;">=</span>"<span style="color: blue;">PART_ClearFilterButton</span>"<span style="color: blue;"> </span><span style="color: red;">Property</span><span style="color: blue;">=</span>"<span style="color: blue;">Visibility</span>"<span style="color: blue;"> </span><span style="color: red;">Value</span><span style="color: blue;">=</span>"<span style="color: blue;">Collapsed</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;/</span><span style="color: #a31515;">DataTrigger</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;/</span><span style="color: #a31515;">ControlTemplate.Triggers</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">ControlTemplate</span><span style="color: blue;">&gt;</span></pre>
</div>
<p>Now we want to be able to handle when a user clicks on the &#8216;Clear Filter&#8217; button.  You do this by overriding the OnApplyTemplate method.  You can get a reference to your controls by calling GetTemplateChild and passing the name of the control.  When the user clicks the &#8216;Clear Filter&#8217; button we just want to remove any text in the TextBox.  We&#8217;ll use the same strategy to get a reference to the TextBox control.</p>
<div style="border-right:white 2px ridge;padding-right:10px;border-top:white 2px ridge;padding-left:10px;font-size:10pt;background:#eeeeee;padding-bottom:10px;margin:10px;border-left:white 2px ridge;color:black;padding-top:10px;border-bottom:white 2px ridge;font-family:consolas, courier new;word-wrap:break-word;">
<pre style="margin:0px;"><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> OnApplyTemplate()</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: blue;">base</span>.OnApplyTemplate();</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;">    <span style="color: #2b91af;">Button</span> clearFilterButton = <span style="color: blue;">base</span>.GetTemplateChild(<span style="color: #a31515;">"PART_ClearFilterButton"</span>) <span style="color: blue;">as</span> <span style="color: #2b91af;">Button</span>;</pre>
<pre style="margin:0px;">    <span style="color: blue;">if</span> (clearFilterButton != <span style="color: blue;">null</span>)</pre>
<pre style="margin:0px;">    {</pre>
<pre style="margin:0px;">        clearFilterButton.Click += <span style="color: blue;">new</span> <span style="color: #2b91af;">RoutedEventHandler</span>(ClearFilterButton_Click);</pre>
<pre style="margin:0px;">    }</pre>
<pre style="margin:0px;">}</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;"><span style="color: blue;">private</span> <span style="color: blue;">void</span> ClearFilterButton_Click(<span style="color: #2b91af;">Object</span> sender, <span style="color: #2b91af;">RoutedEventArgs</span> e)</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: #2b91af;">TextBox</span> textBox = <span style="color: blue;">base</span>.GetTemplateChild(<span style="color: #a31515;">"PART_FilterTextBox"</span>) <span style="color: blue;">as</span> <span style="color: #2b91af;">TextBox</span>;</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;">    <span style="color: blue;">if</span> (textBox != <span style="color: blue;">null</span>)</pre>
<pre style="margin:0px;">    {</pre>
<pre style="margin:0px;">        textBox.Text = <span style="color: #2b91af;">String</span>.Empty;</pre>
<pre style="margin:0px;">    }</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>In the WPF Application project add a reference to the custom control project.</p>
<p><a href="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/AddReference.png"><img src="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/AddReference_thumb.png" border="0" alt="Add Reference" width="545" height="438" /></a></p>
<p>Now you can create a namespace for the controls project, labeled as controls here, and add your control to the form.</p>
<div style="border-right:white 2px ridge;padding-right:10px;border-top:white 2px ridge;padding-left:10px;font-size:10pt;background:#eeeeee;padding-bottom:10px;margin:10px;border-left:white 2px ridge;color:black;padding-top:10px;border-bottom:white 2px ridge;font-family:consolas, courier new;word-wrap:break-word;">
<pre style="margin:0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">Window</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    </span><span style="color: red;">x:Class</span><span style="color: blue;">=</span>"<span style="color: blue;">FilterTextBoxDemo.MainWindow</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">    </span><span style="color: red;">xmlns</span><span style="color: blue;">=</span>"<span style="color: blue;">http://schemas.microsoft.com/winfx/2006/xaml/presentation</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">    </span><span style="color: red;">xmlns:x</span><span style="color: blue;">=</span>"<span style="color: blue;">http://schemas.microsoft.com/winfx/2006/xaml</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">    </span><span style="color: red;">xmlns:controls</span><span style="color: blue;">=</span>"<span style="color: blue;">clr-namespace:FilterTextBox;assembly=FilterTextBox</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">    </span><span style="color: red;">Title</span><span style="color: blue;">=</span>"<span style="color: blue;">FilterTextBox Demo</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">    </span><span style="color: red;">Height</span><span style="color: blue;">=</span>"<span style="color: blue;">300</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">    </span><span style="color: red;">Width</span><span style="color: blue;">=</span>"<span style="color: blue;">300</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;</span><span style="color: #a31515;">StackPanel</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;</span><span style="color: #a31515;">controls:FilterTextBox</span><span style="color: blue;"> /&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;/</span><span style="color: #a31515;">StackPanel</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">Window</span><span style="color: blue;">&gt;</span></pre>
</div>
<p>And we have a working control!</p>
<p><a href="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/FilterTextBoxDemo3_3.png"><img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" src="http://xamlcoder.com/cs/blogs/joe/images/PostTitle_7DC5/FilterTextBoxDemo3_thumb_3.png" border="0" alt="Filter TextBox Demo" width="310" height="141" /></a></p>
<p>To make sure that the control still works when templated, lets go ahead and make a simple template for it.</p>
<div style="border-right:white 2px ridge;padding-right:10px;border-top:white 2px ridge;padding-left:10px;font-size:10pt;background:#eeeeee;padding-bottom:10px;margin:10px;border-left:white 2px ridge;color:black;padding-top:10px;border-bottom:white 2px ridge;font-family:consolas, courier new;word-wrap:break-word;">
<pre style="margin:0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">Window</span><span style="color: blue;"> </span><span style="color: red;">...</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;</span><span style="color: #a31515;">Window.Resources</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;</span><span style="color: #a31515;">Style</span><span style="color: blue;"> </span><span style="color: red;">x:Key</span><span style="color: blue;">=</span>"<span style="color: blue;">LOCAL_MyStyle</span>"<span style="color: blue;"> </span><span style="color: red;">TargetType</span><span style="color: blue;">=</span>"<span style="color: blue;">{x:Type controls:FilterTextBox}</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      &lt;</span><span style="color: #a31515;">Setter</span><span style="color: blue;"> </span><span style="color: red;">Property</span><span style="color: blue;">=</span>"<span style="color: blue;">BorderBrush</span>"<span style="color: blue;"> </span><span style="color: red;">Value</span><span style="color: blue;">=</span>"<span style="color: blue;">CornFlowerBlue</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      &lt;</span><span style="color: #a31515;">Setter</span><span style="color: blue;"> </span><span style="color: red;">Property</span><span style="color: blue;">=</span>"<span style="color: blue;">BorderThickness</span>"<span style="color: blue;"> </span><span style="color: red;">Value</span><span style="color: blue;">=</span>"<span style="color: blue;">2</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      &lt;</span><span style="color: #a31515;">Setter</span><span style="color: blue;"> </span><span style="color: red;">Property</span><span style="color: blue;">=</span>"<span style="color: blue;">Template</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">        &lt;</span><span style="color: #a31515;">Setter.Value</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">          &lt;</span><span style="color: #a31515;">ControlTemplate</span><span style="color: blue;"> </span><span style="color: red;">TargetType</span><span style="color: blue;">=</span>"<span style="color: blue;">{x:Type controls:FilterTextBox}</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">            &lt;</span><span style="color: #a31515;">Border</span></pre>
<pre style="margin:0px;"><span style="color: blue;">              </span><span style="color: red;">Background</span><span style="color: blue;">=</span>"<span style="color: blue;">{TemplateBinding Background}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">              </span><span style="color: red;">BorderBrush</span><span style="color: blue;">=</span>"<span style="color: blue;">{TemplateBinding BorderBrush}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">              </span><span style="color: red;">BorderThickness</span><span style="color: blue;">=</span>"<span style="color: blue;">{TemplateBinding BorderThickness}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">              </span><span style="color: red;">CornerRadius</span><span style="color: blue;">=</span>"<span style="color: blue;">10</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">              &lt;</span><span style="color: #a31515;">DockPanel</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                </span><span style="color: red;">LastChildFill</span><span style="color: blue;">=</span>"<span style="color: blue;">True</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">                </span><span style="color: red;">Margin</span><span style="color: blue;">=</span>"<span style="color: blue;">5</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                &lt;</span><span style="color: #a31515;">Button</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                  </span><span style="color: red;">x:Name</span><span style="color: blue;">=</span>"<span style="color: blue;">PART_ClearFilterButton</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">                  </span><span style="color: red;">Content</span><span style="color: blue;">=</span>"<span style="color: blue;">--</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">                  </span><span style="color: red;">Width</span><span style="color: blue;">=</span>"<span style="color: blue;">30</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">                  </span><span style="color: red;">ToolTip</span><span style="color: blue;">=</span>"<span style="color: blue;">Clear Filter</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">                  </span><span style="color: red;">DockPanel.Dock</span><span style="color: blue;">=</span>"<span style="color: blue;">Left</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                &lt;</span><span style="color: #a31515;">TextBox</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                  </span><span style="color: red;">x:Name</span><span style="color: blue;">=</span>"<span style="color: blue;">PART_FilterTextBox</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">                  </span><span style="color: red;">Text</span><span style="color: blue;">=</span>"<span style="color: blue;">{Binding Path=Text,</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                          Mode=TwoWay,</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                          UpdateSourceTrigger=PropertyChanged,</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                          RelativeSource={RelativeSource TemplatedParent}}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">                  </span><span style="color: red;">BorderBrush</span><span style="color: blue;">=</span>"<span style="color: blue;">{x:Null}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">                  </span><span style="color: red;">BorderThickness</span><span style="color: blue;">=</span>"<span style="color: blue;">0</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">                  </span><span style="color: red;">VerticalAlignment</span><span style="color: blue;">=</span>"<span style="color: blue;">Center</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">              &lt;/</span><span style="color: #a31515;">DockPanel</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">            &lt;/</span><span style="color: #a31515;">Border</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">            &lt;</span><span style="color: #a31515;">ControlTemplate.Triggers</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">              &lt;</span><span style="color: #a31515;">DataTrigger</span><span style="color: blue;"> </span><span style="color: red;">Binding</span><span style="color: blue;">=</span>"<span style="color: blue;">{Binding Path=Text.Length, ElementName=PART_FilterTextBox}</span>"<span style="color: blue;"> </span><span style="color: red;">Value</span><span style="color: blue;">=</span>"<span style="color: blue;">0</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                &lt;</span><span style="color: #a31515;">Setter</span><span style="color: blue;"> </span><span style="color: red;">TargetName</span><span style="color: blue;">=</span>"<span style="color: blue;">PART_ClearFilterButton</span>"<span style="color: blue;"> </span><span style="color: red;">Property</span><span style="color: blue;">=</span>"<span style="color: blue;">Visibility</span>"<span style="color: blue;"> </span><span style="color: red;">Value</span><span style="color: blue;">=</span>"<span style="color: blue;">Collapsed</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">              &lt;/</span><span style="color: #a31515;">DataTrigger</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">            &lt;/</span><span style="color: #a31515;">ControlTemplate.Triggers</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">          &lt;/</span><span style="color: #a31515;">ControlTemplate</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">        &lt;/</span><span style="color: #a31515;">Setter.Value</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      &lt;/</span><span style="color: #a31515;">Setter</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;/</span><span style="color: #a31515;">Style</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;/</span><span style="color: #a31515;">Window.Resources</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;</span><span style="color: #a31515;">StackPanel</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;</span><span style="color: #a31515;">controls:FilterTextBox</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">BorderBrush</span><span style="color: blue;">=</span>"<span style="color: blue;">#ACBFE4</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">Margin</span><span style="color: blue;">=</span>"<span style="color: blue;">10</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">TextChanged</span><span style="color: blue;">=</span>"<span style="color: blue;">FilterTextBox_TextChanged</span>"<span style="color: blue;">/&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;</span><span style="color: #a31515;">controls:FilterTextBox</span></pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">Style</span><span style="color: blue;">=</span>"<span style="color: blue;">{StaticResource LOCAL_MyStyle}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">Margin</span><span style="color: blue;">=</span>"<span style="color: blue;">10</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">      </span><span style="color: red;">TextChanged</span><span style="color: blue;">=</span>"<span style="color: blue;">FilterTextBox_TextChanged</span>"<span style="color: blue;">/&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">  &lt;/</span><span style="color: #a31515;">StackPanel</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">Window</span><span style="color: blue;">&gt;</span></pre>
</div>
<p>Hope that helps!</p>
<p>Joe</p>
<p><img src="http://xamlcoder.com/cs/aggbug.aspx?PostID=811" alt="" width="1" height="1" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/building-custom-template-able-wpf-controls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
