<?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; AutoCompleteBox</title>
	<atom:link href="http://blogs.veracitysolutions.com/tag/autocompletebox/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 Get a Silverlight 3 AutoCompleteBox To Show Sample Data in Blend 3</title>
		<link>http://blogs.veracitysolutions.com/how-to-get-a-silverlight-3-autocompletebox-to-show-sample-data-in-blend-3/</link>
		<comments>http://blogs.veracitysolutions.com/how-to-get-a-silverlight-3-autocompletebox-to-show-sample-data-in-blend-3/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 15:31:00 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[AutoCompleteBox]]></category>
		<category><![CDATA[Sample Data]]></category>
		<category><![CDATA[Silverlight 3]]></category>

		<guid isPermaLink="false">http://blogs.veracitysolutions.com/?p=610</guid>
		<description><![CDATA[Download Project Files for AutoCompleteBox Sample Data Project
So you’re playing around with Silverlight in Blend 3 and you set up some sample data. (If you don’t know how to do that, I’ll get to that in this tutorial too.) Your sample data is all set up and you’ve attached it to your AutoComplete box… but [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.designersilverlight.com/wp-content/uploads/2009/08/autocompleteboxsampledatatutorial.zip'>Download Project Files for AutoCompleteBox Sample Data Project</a></p>
<p>So you’re playing around with Silverlight in Blend 3 and you set up some sample data. (If you don’t know how to do that, I’ll get to that in this tutorial too.) Your sample data is all set up and you’ve attached it to your AutoComplete box… but nothing happens. You get angry and throw your computer out a window (unless it’s a MacBook, in which case you apologize to it for flirting with Microsoft technologies and take it for a little cuddle), but you eventually think you want to try again and so you go looking for the problem on the internet. Hopefully that is what brought you here.</p>
<p>I talk too much, I know.</p>
<p>Just to start out at the beginning, if you didn’t know that Silverlight 3 had an AutoCompleteBox, that’s because it isn’t a default control. You can install it and several other fantastic controls <a href="http://www.codeplex.com/Silverlight">via the Silverlight Toolkit</a>. If you don’t have this installed, do so now.</p>
<p>With the Silverlight Toolkit installed, let’s just make sure we have the right problem.</p>
<p>OK… lets create our sample data. In the top right corner of Blend, you’ll see a couple of tabs. Click on the “Data” tab and click on the “Add sample data” menu and choose “Define New Sample Data…”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image001.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image001-thumb.png" width="420" height="97" /></a></p>
<p>Name your database something short (you’ll see why later) and rename the “Collection” collection to something less confusing, like “Sample”. Add some string properties and give them handy names. You can see my sample data as a starting point…</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00151.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[5]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0015-thumb1.png" width="234" height="118" /></a></p>
<p>That’ll do for now. Now, draw an AutoCompleteBox into your UserControl and drag the Sample collection on top of it. It will set itself to be the default ItemsSource. Now, hit F5 to run your application. It should act like this one below… if you type stuff, nothing comes up… but if you type “e”, you can see (in my sample, at least), it shows a set of items labeled, “Expression.Blend.SampleData.AutoData.SampleItem”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00114.png"><img style="border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px" border="0" alt="clip_image001[1]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0011-thumb1.png" width="318" height="125" /></a></p>
<p>This is because the filter being used by default in the AutoCompleteBox takes the items and converts them directly to strings. If the item itself is an object (like this example and like most real data you’re probably using), it will convert it into the string representation of that data type.</p>
<p>So let’s fix it. First, we’re going to create an ItemTemplate for our AutoCompleteBox so that we can actually see the data in it. The quickest way to do this is actually kind of silly. Create a ListBox in your app and drag the data collection into it. It will auto-generate a super simple ItemTemplate. Then right-click on your AutoCompleteBox and go to “Edit Additional Templates –&gt; Edit ItemTemplate –&gt; Apply Resource –&gt; [name of generated template here]”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00171.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[7]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0017-thumb1.png" width="609" height="132" /></a></p>
<p>Delete your ListBox, just to clean things up.</p>
<p>Now you’ll have an AutoCompleteBox that shows the data like you want it, but it’s still looking at the “Expression.Blend.SampleData.SampleItem” when you type in the box, so it’s not acting the way we want. In order to do that, we just have to override the “ToString()” method in the AutoData. Open up “AutoData.xaml.cs” </p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0011.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[1]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0011-thumb.png" width="234" height="150" /></a></p>
<p>Scroll down to the “SampleItem” class and make the beginning of that class look like this:</p>
<pre><span style="color: blue">public class </span><span style="color: #2b91af">SampleItem </span>: System.ComponentModel.<span style="color: #2b91af">INotifyPropertyChanged
</span>{
    <span style="color: blue">public override string </span>ToString()
    {
        <span style="color: blue">return </span>Name;
    }</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>If you want to use another property as your text return, just use that property name instead of Name. If you want to use a combination of properties, you could do something like this:</p>
<pre><span style="color: blue">return </span>Name + <span style="color: #a31515">&quot; - &quot; </span>+ CompanyName;</pre>
<p>Although, if you want it to look at two fields, you should change the FilterMode to “Contains”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0013.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[3]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0013-thumb.png" width="255" height="72" /></a></p>
<p>And… tada! You’re done… unless you want to give it some styling to better enhance the user experience (like putting the name at the top so that it draws the eye).</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00131.png"><img style="border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px" border="0" alt="clip_image001[3]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0013-thumb1.png" width="201" height="164" /></a></p>
<p>Also valuable, check out Tim Heuer’s post on turning <a href="https://timheuer.com/blog/archive/2008/11/05/silverlight-editable-combobox-using-styles.aspx">the AutoCompleteBox into an editable ComboBox</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/how-to-get-a-silverlight-3-autocompletebox-to-show-sample-data-in-blend-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
