<?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; Combobox</title>
	<atom:link href="http://blogs.veracitysolutions.com/category/combobox/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 Wrap Text in a Silverlight 3 ComboBox</title>
		<link>http://blogs.veracitysolutions.com/how-to-wrap-text-in-a-silverlight-3-combobox/</link>
		<comments>http://blogs.veracitysolutions.com/how-to-wrap-text-in-a-silverlight-3-combobox/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 22:34:04 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Combobox]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Styles]]></category>
		<category><![CDATA[Templates]]></category>

		<guid isPermaLink="false">http://blogs.veracitysolutions.com/?p=605</guid>
		<description><![CDATA[Download ComboBox TextWrapping Tutorial Project
Sometimes you just want stuff to work the way you want it to work. I want a property on the Silverlight ComboBox that says “WrapText”. But alas there is not one, so I have to do it myself. 
So your ComboBox looks like this:
and you want it to look like this:

Let’s [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.designersilverlight.com/wp-content/uploads/2009/08/textwrappingcombobox.zip'>Download ComboBox TextWrapping Tutorial Project</a></p>
<p>Sometimes you just want stuff to work the way you want it to work. I want a property on the Silverlight ComboBox that says “WrapText”. But alas there is not one, so I have to do it myself. </p>
<p>So your ComboBox looks like this:</p>
<p>and you want it to look like this:</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00113.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[13]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00113-thumb.png" width="193" height="83" /></a></p>
<p>Let’s make that happen.</p>
<p>First, we need to make sure that the DataTemplate is going to act in such a way that it is compatible to having the data inside it wrap.</p>
<p>So… right-click on your ComboBox and go to: “Edit Additional Templates –&gt; Edit Generated Items (Item Template) –&gt; Edit Current”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0015.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-thumb.png" width="605" height="74" /></a></p>
<p>If that option isn’t available, go to “Create Empty” The DataTemplate in my sample looks like this:</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0017.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-thumb.png" width="128" height="72" /></a></p>
<p>Hopefully, you have the binding information available for your data. If not, check out this post on creating sample data for the purpose of styling things. Make sure all the Widths and Heights (but especially the Widths) are set to “Auto”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0019.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[9]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0019-thumb.png" width="199" height="55" /></a></p>
<p>And make sure your TextBox or TextBlock has TextWrapping=”Wrap”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00111.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[11]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00111-thumb.png" width="225" height="31" /></a></p>
<p>So now your ComboBox wraps in the main section… which is awesome, but you still have this happen when you open up the DropDown (PopUp)</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00115.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[15]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00115-thumb.png" width="539" height="99" /></a></p>
<p>If you want that to wrap as well, the solution isn’t hard so we’ll deal with it here instead of giving it another post.</p>
<p>First,change the HorizontalContentAlignment on the ComboBox to be “Stretch”. </p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00117.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[17]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00117-thumb.png" width="259" height="58" /></a></p>
<p>Next, right click on the ComboBox and go to “Edit Additional Templates –&gt; Edit Generated Item Container (ItemContainerStyle) –&gt; Edit a Copy…”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00119.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[19]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00119-thumb.png" width="597" height="88" /></a></p>
<p>What is happening is that the ItemContainerStyle element is requesting all the space in the world from it’s parent and since the parent with the ultimate authority is a PopUp, it sees no reason to constrict the ItemContainerStyle with irritating constraints, so the text element finds no reason to start wrapping. So, we’re going to tell the ItemContainerStyle that it can only request a certain amount of space. In this particular case, we’re going to set MaxWidth=”200”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00121.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[21]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00121-thumb.png" width="211" height="30" /></a></p>
<p>And we get this:</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/finalcomboboxwrapping.png"><img src="http://www.designersilverlight.com/wp-content/uploads/2009/08/finalcomboboxwrapping.png" alt="finalcomboboxwrapping" width="235" height="230" class="alignnone size-full wp-image-723" /></a>
<p>And we’re happy.</p>
<p> Or at least I hope we’re happy. If you’re not happy, mention your problem in the comments and I’ll try to get back to you with any tweaks or updates you need.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/how-to-wrap-text-in-a-silverlight-3-combobox/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Styling A ComboBox In Silverlight 3</title>
		<link>http://blogs.veracitysolutions.com/styling-a-combobox-in-silverlight-3/</link>
		<comments>http://blogs.veracitysolutions.com/styling-a-combobox-in-silverlight-3/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 15:24:56 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Combobox]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Styles]]></category>
		<category><![CDATA[Silverlight 3]]></category>
		<category><![CDATA[styling]]></category>

		<guid isPermaLink="false">http://blogs.veracitysolutions.com/?p=590</guid>
		<description><![CDATA[Download ComboBox Styling Project Files
This post is meant to be a center point for topics related to styling the Silverlight 3 ComboBox. It will go over the basic about ComboBox styling and will also get updated with links to tutorials about common design problems with the ComboBox. 
So, let’s get started…
If you want to change [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.designersilverlight.com/wp-content/uploads/2009/08/comboboxstylingbasics.zip'>Download ComboBox Styling Project Files</a></p>
<p>This post is meant to be a center point for topics related to styling the Silverlight 3 ComboBox. It will go over the basic about ComboBox styling and will also get updated with links to tutorials about common design problems with the ComboBox. </p>
<p>So, let’s get started…</p>
<p>If you want to change the look of the main ComboBox or the ComboBox drop down (popup), </p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0016.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[6]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0016-thumb.png" width="162" height="50" /></a></p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0014.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[4]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0014-thumb.png" width="182" height="150" /></a></p>
<p>you’re going to want to change the main template of the ComboBox.</p>
</p>
<p>As an aside, in order to make this as simple as possible, we’re not going to connect out ComboBox to any data… we’re just going to add a bunch of ComboBoxItems to it. To do this, click on the double-arrow at the bottom of the toolbar and type “combo” into the search field. </p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00110.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[10]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00110-thumb.png" width="339" height="155" /></a> </p>
<p>Click on the ComboBoxItem and draw it into the ComboBox a couple times. This will give your items some weird heights and widths, so change those if that bothers you. We now have enough items to be able to see the drop down for the purposes of styling it.</p>
<p>OK… let’s get started with actually working on the ComboBox template. Right click on the ComboBox and going to “Edit Template –&gt; Edit a Copy…”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0018.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[8]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image0018-thumb.png" width="321" height="72" /></a></p>
</p>
<p>This will dump a copy of the default ComboBox style and template into your resources that you can play around with.</p>
<p>Let’s say we want to do something as simple as changing the corners in the main comboBox and the drop down to make them a little more rounded.</p>
<p>When we look at the template, we should see something that looks like this:</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00112.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[12]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00112-thumb.png" width="307" height="264" /></a></p>
<p>Let’s start with rounding the corners of the pop-up, since we can do that from right here. On the PopupBorder element, let’s change the CornerRadius to “0,0,16,16”. And let’s change the border color and thickness just cause we wanna. We end up with something like this:</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00116.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[16]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00116-thumb.png" width="148" height="224" /></a></p>
<p>Now, lets work on changing the corners on the rest of the ComboBox. As it turns out, most of the visual elements in the ComboBox are in the DropDownToggle ToggleButton and go to ControlTemplate by right clicking on it and going to “Edit Template –&gt; Edit Current”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00118.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[18]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00118-thumb.png" width="323" height="71" /></a></p>
<p>Now, we can see 8 rectangles that make up the design of the ComboBox. If you wanted to simplify the design, you could get rid of them and put something else in, but we’re going to assume for the moment that you just want to change the corners. Go through the rectangles and if RadiusX and RadiusY are 3, change them to 16 an if they are 2, change them to 15.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00120.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[20]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00120-thumb.png" width="198" height="57" /></a>&#160; becomes <a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00122.png"><img style="border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px" border="0" alt="clip_image001[22]" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip-image00122-thumb.png" width="200" height="58" /></a></p>
</p>
<p>And you have something that is… not that attractive, but it is definitely different.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/styling-a-combobox-in-silverlight-3/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How To Use a ListView or DataGrid In a ComboBox Drop Down (Without A Line Of Code)</title>
		<link>http://blogs.veracitysolutions.com/how-to-use-a-listview-or-datagrid-in-a-combobox-drop-down-without-a-line-of-code/</link>
		<comments>http://blogs.veracitysolutions.com/how-to-use-a-listview-or-datagrid-in-a-combobox-drop-down-without-a-line-of-code/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 00:30:00 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Combobox]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[ListView]]></category>
		<category><![CDATA[Styles]]></category>
		<category><![CDATA[Templates]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.designerwpf.com/2008/10/01/how-to-use-a-listview-or-datagrid-in-a-combobox-drop-down-without-a-line-of-code/</guid>
		<description><![CDATA[Super cool new thing I learned today. In WPF, we can make it so that the drop down (popup) on a ComboBox displays the data in a ListView. Because of the fact that I believe the DataGrid will eventually overtake the ListView as the gold standard for data display in WPF, we’ll do it both [...]]]></description>
			<content:encoded><![CDATA[<p>Super cool new thing I learned today. In WPF, we can make it so that the drop down (popup) on a ComboBox displays the data in a ListView. Because of the fact that I believe the DataGrid will eventually overtake the ListView as the gold standard for data display in WPF, we’ll do it both ways.</p>
<p>This tutorial is done entirely in Blend and without a line of code.</p>
<p><strong>Step 0)</strong> (for the DataGrid only)</p>
<p>Go to Code Plex and <a href="http://www.codeplex.com/wpf/Release/ProjectReleases.aspx?ReleaseId=14963">download the WPF Toolkit</a>. Extract to a convenient location.</p>
<p>Right-Click on the References folder in your project tab and click “Add Reference…”</p>
<p><a href="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image001.png"><img src="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image001-thumb.png" title="clip_image001" style="border-width: 0px; display: inline" alt="clip_image001" border="0" height="85" width="238" /></a></p>
<p>Navigate to the location you extracted the WPFToolkit.dll file, select it and hit OK.<br />
<a href="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image0016.png"><img src="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image0016-thumb.png" title="clip_image001[6]" style="border-width: 0px; display: inline" alt="clip_image001[6]" border="0" height="205" width="177" /></a></p>
<p>Step 1) Select the ComboBox you wish to change and edit the ControlTemplate by right-clicking and selecting “Edit Control Parts (Template) –&gt; Edit a Copy…”</p>
<p><a href="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image0011.png"><img src="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image0011-thumb.png" title="clip_image001[1]" style="border: 0px none ; display: inline" alt="clip_image001[1]" border="0" height="55" width="328" /></a></p>
<p>Step 2) Find the ItemsPresenter. This is what would normally display our ItemsSource.</p>
<p><a href="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image0013.png"><img src="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image0013-thumb.png" title="clip_image001[3]" style="border: 0px none ; display: inline" alt="clip_image001[3]" border="0" height="187" width="252" /></a></p>
<p>We’re going to get rid of it. And the ScrollViewer for good measure.</p>
<p>Step 3) Where the ScrollViewer is, put in a ListView or a DataGrid, whichever one you’re using.</p>
<p><a href="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image0015.png"><img src="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image0015-thumb.png" title="clip_image001[5]" style="border: 0px none ; display: inline" alt="clip_image001[5]" border="0" height="132" width="255" /></a></p>
<p>Now, go the properties of that ListView or DataGrid and click on the box to the right of “ItemsSource”</p>
<p><a href="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image00111.png"><img src="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image00111-thumb.png" title="clip_image001[11]" style="border: 0px none ; display: inline" alt="clip_image001[11]" border="0" height="60" width="168" /></a></p>
<p>and, in the resulting menu, select “Template Binding –&gt; ItemsSource”.</p>
<p><a href="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image0017.png"><img src="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image0017-thumb.png" title="clip_image001[7]" style="border: 0px none ; display: inline" alt="clip_image001[7]" border="0" height="294" width="438" /></a></p>
<p>Set the DataContext of the ListView or DataGrid to the DataContext of the parent ComboBox using the same process.</p>
<p>And… you’re done! Open the ComboBox and you will see that you can select items in the ListView or DataGrid in the ComboBox dropdown and see those items change the selection of the ComboBox.</p>
<p><a href="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image00113.png"><img src="http://blogs.veracitysolutions.com/wp-content/uploads/2008/10/clip-image00113-thumb.png" title="clip_image001[13]" style="border: 0px none ; display: inline" alt="clip_image001[13]" border="0" height="193" width="545" /></a></p>
<p>You’ll notice that this is probably not yet an ideal solution. For example, when we select an item, the dropdown doesn’t automatically close. Your best bet is to use the SelectionChanged event to trigger some logic to close the ComboBox dropdown.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/how-to-use-a-listview-or-datagrid-in-a-combobox-drop-down-without-a-line-of-code/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Building a Silverlight ComboBox Using Attached Behaviors</title>
		<link>http://blogs.veracitysolutions.com/building-silverlight-combobox-using-attached-behaviors/</link>
		<comments>http://blogs.veracitysolutions.com/building-silverlight-combobox-using-attached-behaviors/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 09:46:40 +0000</pubDate>
		<dc:creator>Joe McBride</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Combobox]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">f86693b4-c59e-43d4-8c4a-670d55e24c19:827</guid>
		<description><![CDATA[I recently needed to use a ComboBox in an application I was writing.  Because there is no built-in ComboBox in Silverlight I decided to explore building one using attached behaviors.  If you’re not familiar with this design pattern, check out Nikhil’s posts.  My ComboBox behavior is loosely based on his AutoComplete behavior.  I also make [...]]]></description>
			<content:encoded><![CDATA[<p>I recently needed to use a ComboBox in an application I was writing.  Because there is no built-in ComboBox in Silverlight I decided to explore building one using attached <a href="http://www.nikhilk.net/Silverlight-Behaviors.aspx" target="_blank">behaviors</a>.  If you’re not familiar with this design pattern, check out <a href="http://www.nikhilk.net" target="_blank">Nikhil’s</a> <a href="http://www.nikhilk.net/Silverlight-AutoComplete.aspx" target="_blank">posts</a>.  My ComboBox behavior is loosely based on his <a href="http://www.nikhilk.net/Silverlight-AutoComplete.aspx" target="_blank">AutoComplete</a> behavior.  I also make use of <a href="http://blogs.southworks.net/jdominguez">Julian’s</a> ButtonCommands class, which he describes <a href="http://blogs.southworks.net/jdominguez/2008/08/icommand-for-silverlight-with-attached-behaviors/" target="_blank">in this post</a>.</p>
<p><a href="http://xamlcoder.com/cs/blogs/joe/images/BuildingaSilverlightComboBoxUsingAttache_1C75/SilverlightProjectTestPageMozillaFirefox2.png"><img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" title="Silverlight ComboBox Project Test Page - Mozilla Firefox" src="http://xamlcoder.com/cs/blogs/joe/images/BuildingaSilverlightComboBoxUsingAttache_1C75/SilverlightProjectTestPageMozillaFirefox2_thumb.png" border="0" alt="Silverlight ComboBox Project Test Page - Mozilla Firefox" width="473" height="297" /></a></p>
<p>I started with a simple behavior interface:</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: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin:0px;"><span style="color: gray;">///</span><span style="color: green;"> Represents a contract for encapsulation of logic that can be added</span></pre>
<pre style="margin:0px;"><span style="color: gray;">///</span><span style="color: green;"> to a dependency object through a pattern of attachment.</span></pre>
<pre style="margin:0px;"><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin:0px;"><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;typeparam name="T"&gt;&lt;/typeparam&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IBehavior</span>&lt;T&gt; <span style="color: blue;">where</span> T : <span style="color: #2b91af;">DependencyObject</span></pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin:0px;">    <span style="color: gray;">///</span><span style="color: green;"> Gets the associated object.</span></pre>
<pre style="margin:0px;">    <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin:0px;">    <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;value&gt;</span><span style="color: green;">The associated object.</span><span style="color: gray;">&lt;/value&gt;</span></pre>
<pre style="margin:0px;">    T AssociatedObject { <span style="color: blue;">get</span>; }</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;">    <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin:0px;">    <span style="color: gray;">///</span><span style="color: green;"> Attaches to the specified associated object.</span></pre>
<pre style="margin:0px;">    <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin:0px;">    <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="associatedObject"&gt;</span><span style="color: green;">The associated object.</span><span style="color: gray;">&lt;/param&gt;</span></pre>
<pre style="margin:0px;">    <span style="color: blue;">void</span> Attach(T associatedObject);</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;">    <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin:0px;">    <span style="color: gray;">///</span><span style="color: green;"> Detaches from the associated object.</span></pre>
<pre style="margin:0px;">    <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin:0px;">    <span style="color: blue;">void</span> Detach();</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>Which I used to implement a ComboBoxBehavior class.  I chose to use FrameworkElement because I wanted to be able to attach this ComboBox to different controls.  Possibly a TextBlock, a TextBox, a Border … any number of controls.  We’ll come back to this later …</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;">ComboBoxBehavior</span> : <span style="color: #2b91af;">IBehavior</span>&lt;<span style="color: #2b91af;">FrameworkElement</span>&gt;</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    ...</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>After creating the behavior I created a simple static class which had a single attached property of type ComboBoxBehavior.</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;">class</span> <span style="color: #2b91af;">Behaviors</span></pre>
<pre style="margin:0px;">{</pre>
<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> ComboBoxProperty =</pre>
<pre style="margin:0px;">        <span style="color: #2b91af;">DependencyProperty</span>.RegisterAttached(</pre>
<pre style="margin:0px;">            <span style="color: #a31515;">"ComboBox"</span>,</pre>
<pre style="margin:0px;">            <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">ComboBoxBehavior</span>),</pre>
<pre style="margin:0px;">            <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">Behaviors</span>),</pre>
<pre style="margin:0px;">            <span style="color: blue;">new</span> <span style="color: #2b91af;">PropertyMetadata</span>(</pre>
<pre style="margin:0px;">                <span style="color: blue;">new</span> <span style="color: #2b91af;">PropertyChangedCallback</span>(OnComboBoxChanged)</pre>
<pre style="margin:0px;">            )</pre>
<pre style="margin:0px;">        );</pre>
<pre style="margin:0px;">    ...</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>Attach() and Detach() are called in the property changed callback.</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;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> OnComboBoxChanged(</pre>
<pre style="margin:0px;">    <span style="color: #2b91af;">FrameworkElement</span> element,</pre>
<pre style="margin:0px;">    <span style="color: #2b91af;">ComboBoxBehavior</span> oldValue,</pre>
<pre style="margin:0px;">    <span style="color: #2b91af;">ComboBoxBehavior</span> newValue)</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: blue;">if</span> (oldValue != <span style="color: blue;">null</span>)</pre>
<pre style="margin:0px;">    {</pre>
<pre style="margin:0px;">        oldValue.Detach();</pre>
<pre style="margin:0px;">    }</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;">    <span style="color: blue;">if</span> (newValue != <span style="color: blue;">null</span>)</pre>
<pre style="margin:0px;">    {</pre>
<pre style="margin:0px;">        newValue.Attach(element);</pre>
<pre style="margin:0px;">    }</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>You can then wire up the behavior in XAML … I’m using a Border control in this example.</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;">Border</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    </span><span style="color: red;">xmlns:local</span><span style="color: blue;">=</span>"<span style="color: blue;">clr-namespace:SilverlightComboBox</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;</span><span style="color: #a31515;">local:Behaviors.ComboBox</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">        &lt;</span><span style="color: #a31515;">local:ComboBoxBehavior</span></pre>
<pre style="margin:0px;"><span style="color: blue;">            </span><span style="color: red;">Opened</span><span style="color: blue;">=</span>"<span style="color: blue;">SimpleComboBoxBehavior_Opened</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">            </span><span style="color: red;">ItemSelected</span><span style="color: blue;">=</span>"<span style="color: blue;">SimpleComboBoxBehavior_ItemSelected</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">            </span><span style="color: red;">ShowComboBoxCommand</span><span style="color: blue;">=</span>"<span style="color: blue;">{StaticResource ShowComboBoxSimple}</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">        &lt;/</span><span style="color: #a31515;">local:ComboBoxBehavior</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;/</span><span style="color: #a31515;">local:Behaviors.ComboBox</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;</span><span style="color: #a31515;">TextBlock</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;">uxSimpleComboBox</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;">4,0,4,0</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;/</span><span style="color: #a31515;">TextBlock</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>
</div>
<p>So onto the workhorse … the behavior itself.  The way I built this behavior to work is it fires an event, Opened,  when the DropDown is opened, in which you can set the ItemsSource and the SelectedIndex of the ItemsSource, as demonstrated in the following code:</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;">private</span> <span style="color: blue;">void</span> SimpleComboBoxBehavior_Opened(</pre>
<pre style="margin:0px;">    <span style="color: blue;">object</span> sender, <span style="color: #2b91af;">ComboBoxOpenedEventArgs</span> e)</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: #2b91af;">List</span>&lt;<span style="color: blue;">string</span>&gt; strings = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: blue;">string</span>&gt;();</pre>
<pre style="margin:0px;">    strings.Add(<span style="color: #a31515;">"A"</span>);</pre>
<pre style="margin:0px;">    strings.Add(<span style="color: #a31515;">"B"</span>);</pre>
<pre style="margin:0px;">    strings.Add(<span style="color: #a31515;">"C"</span>);</pre>
<pre style="margin:0px;">    strings.Add(<span style="color: #a31515;">"D"</span>);</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;">    e.ItemsSource = strings;</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;">    <span style="color: blue;">var</span> index = strings.IndexOf(uxSimpleComboBox.Text);</pre>
<pre style="margin:0px;">    e.SelectedIndex = index;</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>Once the user selects a value another event is fired, ItemSelected, which can also be handled:</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;">private</span> <span style="color: blue;">void</span> SimpleComboBoxBehavior_ItemSelected(</pre>
<pre style="margin:0px;">    <span style="color: blue;">object</span> sender, <span style="color: #2b91af;">ComboBoxItemSelectedEventArgs</span> e)</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: blue;">if</span> (e.SelectedItem != <span style="color: blue;">null</span>)</pre>
<pre style="margin:0px;">    {</pre>
<pre style="margin:0px;">        uxSimpleComboBox.Text = e.SelectedItem.ToString();</pre>
<pre style="margin:0px;">    }</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>You may be wondering why I took this approach.  I decided to go down the eventing route because as I mentioned previously, I wanted to be able to attach this behavior to lots of different controls.  As such I felt that it should be up to the implementer to display the data as needed.</p>
<p>I also decided to use commands to drive the opening and closing of the DropDown.  This allows the user to declaratively specify when the DropDown is opened or closed.  If you look at the XAML for the behavior I’m wiring up a ShowComboBoxSimple command to the ShowComboBoxCommand.</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: red;">ShowComboBoxCommand</span><span style="color: blue;">=</span>"<span style="color: blue;">{StaticResource ShowComboBoxSimple}</span>"</pre>
</div>
<p>This is an ICommand of type MultiDelegateCommand.  MultiDelegateCommand is a command which can register multiple delegates to call when it is executed.</p>
<p>I registered a MultiDelegateCommand instance in the Application Resources to be able to use it in the markup:</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> Page()</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: #2b91af;">Application</span>.Current.Resources.Add(</pre>
<pre style="margin:0px;">        <span style="color: #a31515;">"ShowComboBoxSimple"</span>,</pre>
<pre style="margin:0px;">        <span style="color: blue;">new</span> <span style="color: #2b91af;">MultiDelegateCommand</span>()</pre>
<pre style="margin:0px;">    );</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;">    ...</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;">    InitializeComponent();</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;">    ...</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>Lastly, I wanted to show the DropDown anytime any portion of the control I’m attaching to is clicked.  This is accomplished by subscribing to the MouseLeftButtonUp event when the associated object is being attached:</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: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin:0px;"><span style="color: gray;">///</span><span style="color: green;"> Attaches to the specified associated object.</span></pre>
<pre style="margin:0px;"><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin:0px;"><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="associatedObject"&gt;</span><span style="color: green;">The associated object.</span><span style="color: gray;">&lt;/param&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">public</span> <span style="color: blue;">void</span> Attach(<span style="color: #2b91af;">FrameworkElement</span> associatedObject)</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    AssociatedObject = associatedObject;</pre>
<pre style="margin:0px;">    AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseLeftButtonUp;</pre>
<pre style="margin:0px;">    AssociatedObject.LostFocus += AssociatedObject_LostFocus;</pre>
<pre style="margin:0px;">}</pre>
</div>
<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;">private</span> <span style="color: blue;">void</span> AssociatedObject_MouseLeftButtonUp(</pre>
<pre style="margin:0px;">    <span style="color: blue;">object</span> sender, <span style="color: #2b91af;">MouseButtonEventArgs</span> e)</pre>
<pre style="margin:0px;">{</pre>
<pre style="margin:0px;">    <span style="color: blue;">if</span> (ShowComboBoxCommand != <span style="color: blue;">null</span> &amp;&amp; !IsDropDownOpen)</pre>
<pre style="margin:0px;">    {</pre>
<pre style="margin:0px;">        ShowComboBoxCommand.Execute();</pre>
<pre style="margin:0px;">    }</pre>
<pre style="margin:0px;">}</pre>
</div>
<p>And that’s basically it!  You may be wondering &#8230; does this control support complex types?  You betcha! The source show’s a sample of working with complex types.  Here is shown a complex City type with a Name property.</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;">DataTemplate</span><span style="color: blue;"> </span><span style="color: red;">x:Key</span><span style="color: blue;">=</span>"<span style="color: blue;">CityTemplate</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;</span><span style="color: #a31515;">TextBlock</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 Name}</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">ToolTipService.ToolTip</span><span style="color: blue;">=</span>"<span style="color: blue;">{Binding Name}</span>"<span style="color: blue;">/&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">DataTemplate</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;">...</pre>
<pre style="margin:0px;"></pre>
<pre style="margin:0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">local:Behaviors.ComboBox</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;</span><span style="color: #a31515;">local:ComboBoxBehavior</span></pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">Opened</span><span style="color: blue;">=</span>"<span style="color: blue;">ComplexComboBoxBehavior_Opened</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">ItemSelected</span><span style="color: blue;">=</span>"<span style="color: blue;">ComplexComboBoxBehavior_ItemSelected</span>"</pre>
<pre style="margin:0px;"><span style="color: blue;">        </span><span style="color: red;">ShowComboBoxCommand</span><span style="color: blue;">=</span>"<span style="color: blue;">{StaticResource ShowComboBoxComplex}</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">        &lt;</span><span style="color: #a31515;">local:ComboBoxBehavior.DropDownTemplate</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">            &lt;</span><span style="color: #a31515;">DataTemplate</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">                &lt;</span><span style="color: #a31515;">ListBox</span><span style="color: blue;"> </span><span style="color: red;">ItemTemplate</span><span style="color: blue;">=</span>"<span style="color: blue;">{StaticResource CityTemplate}</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">            &lt;/</span><span style="color: #a31515;">DataTemplate</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">        &lt;/</span><span style="color: #a31515;">local:ComboBoxBehavior.DropDownTemplate</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">    &lt;/</span><span style="color: #a31515;">local:ComboBoxBehavior</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin:0px;"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">local:Behaviors.ComboBox</span><span style="color: blue;">&gt;</span></pre>
<p>&#8230;</p></div>
<p>Download: <a href="http://xamlcoder.com/joe/downloads/SilverlightComboBox.zip" target="_blank">SilverlightComboBox.zip</a></p>
<p>Hope that helps!</p>
<p>Joe</p>
<p><img src="http://xamlcoder.com/cs/aggbug.aspx?PostID=827" alt="" width="1" height="1" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/building-silverlight-combobox-using-attached-behaviors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Styling the ComboBox Dropdown (popup)</title>
		<link>http://blogs.veracitysolutions.com/styling-the-combobox-dropdown-popup/</link>
		<comments>http://blogs.veracitysolutions.com/styling-the-combobox-dropdown-popup/#comments</comments>
		<pubDate>Tue, 25 Mar 2008 03:36:25 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Blend]]></category>
		<category><![CDATA[Combobox]]></category>
		<category><![CDATA[cornerradius]]></category>
		<category><![CDATA[dropdown]]></category>
		<category><![CDATA[padding]]></category>
		<category><![CDATA[Popup]]></category>

		<guid isPermaLink="false">http://www.designerwpf.com/2008/03/24/styling-the-combobox-dropdown-popup/</guid>
		<description><![CDATA[ This tutorial derives from the general &#8220;How to Style the ComboBox&#8221; set of tutorials.
First let&#8217;s make sure you&#8217;re in the right place. In this tutorial, we&#8217;re going to style the comboBox drop down (also known as the ComboBox popup) seen highlighted in red below.

We will not be styling the items inside the dropdown (highlighted in [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.designerwpf.com/wp-content/uploads/2008/03/cb_dropdown_example.png" title="CB_Image_1"></a> This tutorial derives from the general &#8220;<a href="http://www.designerwpf.com/2008/02/07/the-wpf-designers-guide-to-styling-the-combobox/">How to Style the ComboBox</a>&#8221; set of tutorials.</p>
<p>First let&#8217;s make sure you&#8217;re in the right place. In this tutorial, we&#8217;re going to style the comboBox drop down (also known as the ComboBox popup) seen highlighted in red below.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/03/cb_dropdown_example.png" alt="CB_Image_1" /></p>
<p>We will not be styling the items inside the dropdown (highlighted in blue). You can learn how to do that here.</p>
<p>So… let&#8217;s just go after some of the basics in styling the dropdown. We&#8217;ll give it a new background, a new border and we&#8217;ll round the edges to make it just a little more bubbly.</p>
<p>To start out, we&#8217;ll need to get to our comboBox control template, so right click on the comboBox in the Objects and Timeline window and go to &#8220;<strong>Edit Control Parts (Template) -&gt; Edit a Copy…</strong>&#8221;</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/03/cb_dropdown_edittemplate.png" alt="CB_Image_2" /></p>
<p>Name it something you like and we&#8217;re on our way. </p>
<p>We&#8217;ll be editing the <strong>PART_Popup</strong>. Whatever you do, don&#8217;t change the name to this sucker. Whenever you see a &#8220;PART_Something&#8221;, it is a necessary part of that specific control (hence the naming convention).</p>
<p>The ComboBox dropdown (which we&#8217;ll be calling a popup for the remainder of this post)<span>  </span>is made up of a low cost drop shadow (see <a href="http://www.designerwpf.com/2008/02/08/wpf-drop-shadows-on-the-cheap/">more on that here</a>), a border, a scrollviewer and the itemsPresenter.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/03/cb_dropdown_guts.png" alt="CB_Image_3" /></p>
<p>Most of the standard styling we might want to do is probably going to happen in the Border titled DropDownBorder. We can alter the background and the border brushes easily enough by just changing them in the Properties window. But you may notice some quirky behavior from our scrollviewer when we change the CornerRadius. Below, I&#8217;ve changed the CornerRadius to &#8220;<strong>0,0,10,10</strong>&#8221; and we can see that we lose part of the corner under the scrollviewer.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/03/cb_dropdown_borderconer.png" alt="CB_Image_4" /></p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/03/cb_dropdown_corner_eh.png" alt="CB_Image_5" /></p>
<p>We can solve this easily enough by adding some padding through the Border. Below I&#8217;ve added a padding of &#8220;<strong>0,0,2,6</strong>&#8220;.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/03/cb_dropdown_corner_fixed.png" alt="CB_Image_6" /></p>
<p>Better, but not really ideal. In a perfect world, we would be able to say that the border us able to cut off its content in that nice pretty rounded manner.<span>  </span>(If anyone knows how to do that… let me know, I haven&#8217;t given it hours of thought yet, but I&#8217;d love to know). In this case, however, this sub-optimization is the price we&#8217;re going to have to pay if we don&#8217;t want to have to go in and mess with the scrollbar style and template.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/styling-the-combobox-dropdown-popup/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How Do I Style The ComboBox Items?</title>
		<link>http://blogs.veracitysolutions.com/how-do-i-style-the-combobox-items/</link>
		<comments>http://blogs.veracitysolutions.com/how-do-i-style-the-combobox-items/#comments</comments>
		<pubDate>Fri, 08 Feb 2008 06:45:21 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Blend]]></category>
		<category><![CDATA[Combobox]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[itemcontainerstyle]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[Triggers]]></category>

		<guid isPermaLink="false">http://www.designerwpf.com/2008/02/07/how-do-i-style-the-combobox-items/</guid>
		<description><![CDATA[This is actually a continuation of my post on getting the ComboBox items to accept text wrapping, so I&#8217;ll be working from that point forward. If you&#8217;re coming fresh into this, you won&#8217;t be missing anything&#8230; but that is my explaination for the pictures containing wrapping text.
When last we left our heroes, we has a [...]]]></description>
			<content:encoded><![CDATA[<p>This is actually a continuation of my post on <a href="http://www.designerwpf.com/2008/02/04/how-do-i-wrap-text-in-the-combobox/">getting the ComboBox items to accept text wrapping</a>, so I&#8217;ll be working from that point forward. If you&#8217;re coming fresh into this, you won&#8217;t be missing anything&#8230; but that is my explaination for the pictures containing wrapping text.</p>
<p>When last we left our heroes, we has a couple problems. The first was that our items were either black text on a white background and ran together in a very un-designer-y way.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/basicview.png" alt="BeginningViewComboStyling" /></p>
<p>The second was that the selected item background makes your eyes bleed such a horrid blue color you&#8217;ll feel like Paul Atreides staring at a stone burner.</p>
<p>Was that a little too geek? My apologies.</p>
<p><span id="more-236"></span></p>
<p>Anyway, let&#8217;s do something to make this control a little more usable (as well as easier on our eyes).</p>
<p>Highlight your ComboBox and go up to the toolbar, selecting <strong>Object -&gt; Edit Other Styles -&gt; Edit ItemContainerStyle -&gt; Edit a Copy…</strong></p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/itemcontainerstylemenu.png" alt="ItemContainerStyleMenuCombo" /></p>
<p>We&#8217;re now in the ItemContainerStyle editing mode. I really just need some demarcation between the items, so I&#8217;m going to give the items a BorderBush with a gradient that has a nice blue at the top and fades down to transparency at the bottom. It looks something like this:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/borderbrushgradient.png" alt="BorderBrushGradient" /></p>
<p>We will, of course, want our users to see this border, so let&#8217;s give the style a BorderThickness of 1 all around and a Margin of 4 to offer a little space. Our items now look like this:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/stylework.png" alt="ComboboxItemsStylingWork" /></p>
<p>Better… but we don&#8217;t see the trigger for that hideous shade of blue in the style, so we need to go somewhere else to change it. Right click on the Style and go to <strong>Edit ControlParts (Template) -&gt; Edit Template</strong></p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/templatemenu.png" alt="TemplateMenu" /></p>
<p>We can see that the template here is exceedingly dull… just a border surrounding a ContentPresenter. We like dull. Dull means it&#8217;s not complex. First, let&#8217;s do some polishing up… give that border a CornerRadius of 5. Keep in mind that this is Vista-based development… no sharp corners allowed.</p>
<p>Now, let&#8217;s kill that blue. Click on the <strong>&#8220;IsHighlighted = True&#8221;</strong> trigger at the top…</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/ishighlightedtrigger.png" alt="IsHighlightedTrigger" /></p>
<p>..and we can see the highlighted state. I changed the Background to a nice soft blue gradient (you can change it to a deep purple with a green bubble for all I care… it&#8217;s WPF, go nuts!) This would be great except that I can&#8217;t see the Foreground now, so let&#8217;s change that to make the text readable.</p>
<p> I couldn&#8217;t figure out how to do this in the design mode, so find the IsHighlighted Trigger in the XAML and insert the bold text below:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">Trigger</font> <font color="#ff0000">Property</font>=&#8221;<font color="#0000ff">IsHighlighted</font>&#8221; <font color="#ff0000">Value</font>=&#8221;<font color="#0000ff">true</font>&#8220;<font color="#0000ff">&gt;</font><br />
      <font color="#0000ff">&lt;</font><font color="#800000">Setter</font> <font color="#ff0000">Property</font>=&#8221;<font color="#0000ff">Foreground</font>&#8221; <font color="#ff0000">Value</font>=&#8221;<font color="#0000ff">#FF001720</font>&#8220;<font color="#0000ff">/&gt;<br />
</font>      <font color="#0000ff">&lt;</font><font color="#800000">Setter</font> <font color="#ff0000">Property</font>=&#8221;<font color="#0000ff">Background</font>&#8221; <font color="#ff0000">TargetName</font>=&#8221;<font color="#0000ff">Bd</font>&#8220;<font color="#0000ff">&gt;<br />
      &#8230;</font></p>
<p>Now our ComboBox items look like this:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/finalcomboboxitems.png" alt="FinalComboBoxItems" /></p>
<p>Much better.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/how-do-i-style-the-combobox-items/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The WPF Designers Guide to Styling The ComboBox</title>
		<link>http://blogs.veracitysolutions.com/the-wpf-designers-guide-to-styling-the-combobox/</link>
		<comments>http://blogs.veracitysolutions.com/the-wpf-designers-guide-to-styling-the-combobox/#comments</comments>
		<pubDate>Fri, 08 Feb 2008 05:37:04 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Combobox]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[WPF Designer Guides]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[ContentPresenter]]></category>
		<category><![CDATA[itemcontainerstyle]]></category>
		<category><![CDATA[ItemsPresenter]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[WPF Designer Guide]]></category>

		<guid isPermaLink="false">http://www.designerwpf.com/2008/02/07/the-wpf-designers-guide-to-styling-the-combobox/</guid>
		<description><![CDATA[ The ComboBox is not the most complex of the WPF applications, but it can be a little tricky, so lets do a general overview post of it before we go into the specifics of how we&#8217;re going to make it work.
First of all, if you&#8217;re going to test your comboBox design, you should have it [...]]]></description>
			<content:encoded><![CDATA[<p> The ComboBox is not the most complex of the WPF applications, but it can be a little tricky, so lets do a general overview post of it before we go into the specifics of how we&#8217;re going to make it work.</p>
<p>First of all, if you&#8217;re going to test your comboBox design, you should have it hooked up to an ItemsSource. Don&#8217;t have one? I have a tutorial in which I walk through <a href="http://www.designerwpf.com/2007/12/13/how-do-i-connect-an-rss-feed-to-my-listviewlistboxwhatever/">attaching an RSS feed to your control</a>. It was originally written for the ListView, but it will work fine for a ComboBox.</p>
<p>To start out&#8230; this is your standard ComboBox:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/unalteredcombobox.png" alt="unalteredComboBox" /></p>
<p>When working on a comboBox, you have a couple of options for the Items inside the ComboBox. If the options never change and are not data-driven, you can just toss come ComboBoxItems into it. Otherwise, you can connect it to some kind of ItemsSource (see the link above).</p>
<p>All of my examples are done with a data-driven ComboBoxes, but you should get the desired results if you run through the tutorials with ComboBoxItems.</p>
<p>First, a little bit about the structure of the comboBox.</p>
<p><span id="more-228"></span></p>
<p>The comboBox consists of a couple basic elements. The main part that you would normally click on is a ToggleButton. The information we can see is a ContentPresenter. When you open the ComboBox, you get a popup that is holding an ItemsPresenter.</p>
<p>The ComboBox is a little different inasmuch as it has both a ContentPresenter and an ItemsPresenter, which means that it can take in two different DataTemplates for these different presenters. (If all this is confusing to you, subscribe to my RSS feed… I&#8217;m working on a guide to Styles and Templates which will explain all this in detail.)</p>
<p>Anyway, in the spirit of <a href="http://www.designerwpf.com/2007/12/06/the-wpf-designers-guide-to-styling-the-your-favorite-adjectival-swear-word-here-listview/">my guide to the ListView</a>, I&#8217;m just going to spend the rest of the time guiding you through the basics of how to style the ComboBox. As I get to it, I&#8217;ll write the accompanying articles going into each in detail.</p>
<p><strong>Question 1:</strong> &#8220;How do I style the items in the dropdown?&#8221;</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/comboboxitems.png" alt="ComboItems" /></p>
<p><strong>Answer:</strong> Use the ItemContainerStyle and the accompanying template to define the look and feel of the items.</p>
<p><a href="http://www.designerwpf.com/2008/02/07/how-do-i-style-the-combobox-items/">How to edit the ComboBox ItemContainerStyle in Blend</a></p>
<p><strong>In the XAML:</strong></p>
<p>Put this in the resources:<br />
<font color="#0000ff">&lt;</font><font color="#800000">Style</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">MyComboItemStyle</font>&#8221; <font color="#ff0000">TargetType</font>=&#8221;<font color="#cc9436">{x:Type ComboBoxItem}</font>&#8220;<font color="#0000ff">&gt;</font><br />
<font color="#0000ff">&lt;/</font><font color="#800000">Style</font><font color="#0000ff">&gt;</font></p>
<p>Put this in the composition:<br />
<font color="#0000ff">&lt;</font><font color="#800000">ComboBox</font><font color="#ff0000"> ItemContainerStyle</font>=&#8221;<font color="#cc9436">{DynamicResource MyComboItemStyle}</font>&#8221; <font color="#0000ff">/&gt;</font></p>
<p><strong>Question 2: </strong>&#8220;How do I change the look of the main ComboBox button (highlighted below)?&#8221;</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/comboboxtoggle.png" alt="ComboToggle" /></p>
<p><strong>Answer:</strong> Use the standard Style and Template properties.</p>
<p>How to Style the main ComboBox in Blend (coming soon).</p>
<p><strong>In the XAML:</strong></p>
<p>Put this in the resources:<br />
<font color="#0000ff">&lt;</font><font color="#800000">Style</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">MyComboStyle</font>&#8221; <font color="#ff0000">TargetType</font>=&#8221;<font color="#cc9436">{x:Type ComboBox}</font>&#8220;<font color="#0000ff">&gt;</font><br />
<font color="#0000ff">&lt;/</font><font color="#800000">Style</font><font color="#0000ff">&gt;</font></p>
<p>Put this in the composition:<br />
<font color="#0000ff">&lt;</font><font color="#800000">ComboBox</font><font color="#ff0000"> Style</font>=&#8221;<font color="#cc9436">{DynamicResource MyComboStyle}</font>&#8221; <font color="#0000ff">/&gt;</font></p>
<p><strong>Question 3:</strong> &#8220;How do I style the ComboBox dropdown (highlighted below)?&#8221;</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/comboboxpopup.png" alt="ComboBoxPopup" /></p>
<p><strong>Answer:</strong> Go to the Popup control inside the ComboBox template.</p>
<p><a href="http://www.designerwpf.com/2008/03/24/styling-the-combobox-dropdown-popup/">How to style the ComboBox drop down in Blend.</a></p>
<p>In the XAML:</p>
<p>Put this in the resources:<br />
<font color="#0000ff">&lt;</font><font color="#800000">Style</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">MyComboStyle</font>&#8221; <font color="#ff0000">TargetType</font>=&#8221;<font color="#cc9436">{x:Type ComboBox}</font>&#8220;<font color="#0000ff">&gt;<br />
      &lt;<font color="#800000">Setter</font><font color="#000000"> </font><font color="#ff0000">Property</font><font color="#000000">=&#8221;</font><font color="#0000ff">Template</font><font color="#000000">&#8220;</font></font><font color="#0000ff">&gt;<br />
            &lt;</font><font color="#800000">Setter.Value</font><font color="#0000ff">&gt;<br />
            &#8230;<br />
</font><font color="#0000ff">                  &lt;</font><font color="#800000">Popup</font> <font color="#ff0000">x:Name</font>=&#8221;<font color="#0000ff">PART_Popup</font>&#8221; <font color="#0000ff">&gt;</font><br />
<font color="#0000ff"><font color="#008000"><font color="#0000ff">                        </font>&lt;!&#8211; Edit here &#8211;&gt;<br />
</font>                  &lt;/</font><font color="#800000">Popup</font><font color="#0000ff">&gt;<br />
            &#8230;<br />
</font><font color="#0000ff">            &lt;/</font><font color="#800000">Setter.Value</font><font color="#0000ff">&gt;<br />
      &lt;/<font color="#800000">Setter</font>&gt;<br />
</font><font color="#0000ff">&lt;/<font color="#800000">Style</font><font color="#0000ff">&gt;</font></font></p>
<p>Put this in the composition:<br />
<font color="#0000ff">&lt;</font><font color="#800000">ComboBox</font><font color="#ff0000"> Style</font>=&#8221;<font color="#cc9436">{DynamicResource MyComboStyle}</font>&#8221; <font color="#0000ff">/&gt;</font></p>
<p>There are, of course, other questions related to the ComboBox, but this is just the basics. I&#8217;ll look at some of the more intense ComboBox styling tasks in the near future.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/the-wpf-designers-guide-to-styling-the-combobox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Do I Wrap Text (or Add TextEllipsis) In The ComboBox?</title>
		<link>http://blogs.veracitysolutions.com/how-do-i-wrap-text-in-the-combobox/</link>
		<comments>http://blogs.veracitysolutions.com/how-do-i-wrap-text-in-the-combobox/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 07:28:34 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Blend]]></category>
		<category><![CDATA[Combobox]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://www.designerwpf.com/2008/02/04/how-do-i-wrap-text-in-the-combobox/</guid>
		<description><![CDATA[I&#8217;ve been spending the past several days fighting with the ComboBox in an attempt to make it so something very simple: Wrap text inside the combo box. I&#8217;ve finally figured it out, so I thought I&#8217;d share.
OK, first of all, make sure that your ComboBox is hooked up to something, even if that something is [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been spending the past several days fighting with the ComboBox in an attempt to make it so something very simple: Wrap text inside the combo box. I&#8217;ve finally figured it out, so I thought I&#8217;d share.</p>
<p>OK, first of all, make sure that your ComboBox is hooked up to something, even if that something is some random RSS feed. I have<a href="http://www.designerwpf.com/2007/12/13/how-do-i-connect-an-rss-feed-to-my-listviewlistboxwhatever/"> a post that can help you with that over here</a>. Bind your comboBox to the &#8220;Items&#8221; part of the New York Times RSS feed.</p>
<p>You need to do this because, if you do not, you will have to set the same data template to every single ComboBoxItem that you add to the ComboBox. And that&#8217;s just no fun.</p>
<p>Starting out, your ComboBox should look something like this:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/originalcombobox.png" alt="ComboBoxDefault" /></p>
<p>Right click on your ComboBox and select &#8220;<strong>Edit Other Templates -&gt; Edit Generated Items (ItemTemplate)-&gt; Create Empty…</strong>&#8221; Give your new data template a name and Blend will take you into the Data Template design.</p>
<p><span id="more-216"></span></p>
<p>Put a Grid in your data template and a TextBlock inside that. Set the TextWrapping in your TextBlock to <strong>&#8220;Wrap&#8221;</strong> and set your binding. You can set the binding visually by clicking on the little box to the right of the <strong>&#8220;Text&#8221;</strong> property in the TextBlock and going to the <strong>&#8220;DataBinding&#8221;</strong> option.</p>
<p>In the resulting binding screen, go to the Explicit Data Context tab and click on whatever you want. I chose &#8220;description&#8221; because it is generally long enough to wrap in almost all cases.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/descriptiondatacontext.png" alt="ComboBoxDescriptionBinding" /></p>
<p>My DataTemplate is shown below for reference.</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">DataTemplate</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">WrappingComboTemplate</font>&#8220;<font color="#0000ff">&gt;</font><br />
<font color="#0000ff">&lt;</font><font color="#800000">Grid</font> <font color="#ff0000">Width</font>=&#8221;<font color="#0000ff">Auto</font>&#8221; <font color="#ff0000">Heigh</font>t=&#8221;<font color="#0000ff">Auto</font>&#8220;&gt;<br />
<font color="#0000ff">&lt;</font><font color="#800000">TextBlock</font> <font color="#ff0000">Text</font>=&#8221;<font color="#cc9436">{Binding Mode=OneWay, XPath=description}</font>&#8221; <font color="#ff0000">TextWrapping</font>=&#8221;<font color="#0000ff">Wrap</font>&#8220;<font color="#0000ff">/&gt;<br />
</font>      <font color="#0000ff">&lt;/</font><font color="#800000">Grid</font><font color="#0000ff">&gt;</font><br />
<font color="#0000ff">&lt;/</font><font color="#800000">DataTemplate</font><font color="#0000ff">&gt;</font></p>
<p>If you&#8217;re trying to give your text ellipses, ignore the TextWrapping property and make your TextBlock property to one of the following:</p>
<p><font color="#ff0000">TextTrimming</font>=&#8221;<font color="#0000ff">WordEllipsis</font>&#8221;</p>
<p><font color="#ff0000">TextTrimming</font>=&#8221;<font color="#0000ff">CharacterEllipsis</font>&#8221;</p>
<p>Your combo box should now look something like this:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/comboboxhalfdone.png" alt="ComboBoxTextWrapHalfDone" /></p>
<p>This is good. This is progress. But when you open it, it looks like this:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/comboboxhalfdoneexpanded.png" alt="ComboBoxHalfDoneExpanded" />As you can see, it just keeps going and going. Let&#8217;s fix that.</p>
<p>Right click on your comboBox and go to &#8220;<strong>Edit Control Parts (Template) -&gt; Edit a Copy…&#8221;</strong></p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/controlparttemplatecomboboxtextwrapping.png" alt="ComboBoxControlTemplateCopy" /></p>
<p>Go down to the ScrollViewer that holds the ItemsPresenter.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/comboboxtemplatescrollviewer.png" alt="ComboBoxTemplateScrollViewer" /></p>
<p>Take note… this is an important WPF lesson. Some layouts stretch to contain the items inside them. Unless you put limits on the stretching, they&#8217;ll just go on and on forever. This is really, really bad if you&#8217;re trying to make your text wrap or give your text ellipses because the text element is looking for something that will give it a hard border and an expansive layout won&#8217;t.</p>
<p>This is what has happened here. Because the ScrollViewer HorizontalScrollBarVisibility default is &#8220;Auto&#8221;, the ScrollViewer thinks it has all the space in the world and decides to take advantage of your real-estate generosity.  Lay the smack-down on that insolent ScrollViewer: set the &#8220;HorizontalScrollBarVisibility&#8221; to &#8220;Disabled&#8221;.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/scrollvisibility.png" alt="ScrollViewerVisibility" /></p>
<p>Now, that alone has done exactly nothing. Why? Because the popup that it is placed in still thinks it has all the space in the world. So go to the popup (named PART_Popup) and click on the box next to the &#8220;Width&#8221; property and go to &#8220;DataBinding&#8221;. When the data binding window opens, select the &#8220;Element Property&#8221; tab, then select the fourth item down in the left hand pane and &#8220;ActualWidth&#8221; in the right hand pane.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/databindingpopupwidth.png" alt="DataBindingPopupWidth" /></p>
<p>This will make your comboBox look like this:</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/comboboxalmostdoneexpanded.png" alt="ComboBoxWrapAlmostDonw" /></p>
<p>You can see that the ScrollViewer is slightly cut off… which is irksome. We&#8217;ll solve this by giving our ScrollViewer a &#8220;Margin&#8221; of  &#8220;0,0,4,0&#8243;.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/lastmargintweak.png" alt="LastMarginTweak" /></p>
<p>If this is all you want to do… you&#8217;re done. Me? I can&#8217;t stand the fact that all this text runs together so much.</p>
<p><img src="http://www.designerwpf.com/wp-content/uploads/2008/02/unfinishedpopupstyle.png" alt="UnfinishedPopupStyle" /></p>
<p>The easiest way to deal with is to do some styling back in the DataTemplate we created. The best way to deal with this is to go into the ItemContainerStyle for the ComboBox, which I deal with in my post, <a href="http://www.designerwpf.com/2008/02/07/how-do-i-style-the-combobox-items/">&#8220;Styling ComboBox Items&#8221;.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/how-do-i-wrap-text-in-the-combobox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
