Archive for the ‘combobox’ Category
October 1st, 2008
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.
This tutorial is done entirely in Blend and without a line of code.
Step 0) (for the DataGrid only)
Go to Code Plex and download the WPF Toolkit. Extract to a convenient location.
Right-Click on the References folder in your project tab and click “Add Reference…”

Navigate to the location you extracted the WPFToolkit.dll file, select it and hit OK.
![clip_image001[6] clip_image001[6]](http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0016-thumb.png)
Step 1) Select the ComboBox you wish to change and edit the ControlTemplate by right-clicking and selecting “Edit Control Parts (Template) –> Edit a Copy…”
![clip_image001[1] clip_image001[1]](http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0011-thumb.png)
Step 2) Find the ItemsPresenter. This is what would normally display our ItemsSource.
![clip_image001[3] clip_image001[3]](http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0013-thumb.png)
We’re going to get rid of it. And the ScrollViewer for good measure.
Step 3) Where the ScrollViewer is, put in a ListView or a DataGrid, whichever one you’re using.
![clip_image001[5] clip_image001[5]](http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0015-thumb.png)
Now, go the properties of that ListView or DataGrid and click on the box to the right of “ItemsSource”
![clip_image001[11] clip_image001[11]](http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image00111-thumb.png)
and, in the resulting menu, select “Template Binding –> ItemsSource”.
![clip_image001[7] clip_image001[7]](http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0017-thumb.png)
Set the DataContext of the ListView or DataGrid to the DataContext of the parent ComboBox using the same process.
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.
![clip_image001[13] clip_image001[13]](http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image00113-thumb.png)
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.
March 24th, 2008
This tutorial derives from the general “How to Style the ComboBox” set of tutorials.
First let’s make sure you’re in the right place. In this tutorial, we’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 blue). You can learn how to do that here.
So… let’s just go after some of the basics in styling the dropdown. We’ll give it a new background, a new border and we’ll round the edges to make it just a little more bubbly.
To start out, we’ll need to get to our comboBox control template, so right click on the comboBox in the Objects and Timeline window and go to “Edit Control Parts (Template) -> Edit a Copy…”

Name it something you like and we’re on our way.
We’ll be editing the PART_Popup. Whatever you do, don’t change the name to this sucker. Whenever you see a “PART_Something”, it is a necessary part of that specific control (hence the naming convention).
The ComboBox dropdown (which we’ll be calling a popup for the remainder of this post) is made up of a low cost drop shadow (see more on that here), a border, a scrollviewer and the itemsPresenter.

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’ve changed the CornerRadius to “0,0,10,10” and we can see that we lose part of the corner under the scrollviewer.


We can solve this easily enough by adding some padding through the Border. Below I’ve added a padding of “0,0,2,6“.

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. (If anyone knows how to do that… let me know, I haven’t given it hours of thought yet, but I’d love to know). In this case, however, this sub-optimization is the price we’re going to have to pay if we don’t want to have to go in and mess with the scrollbar style and template.
February 7th, 2008
This is actually a continuation of my post on getting the ComboBox items to accept text wrapping, so I’ll be working from that point forward. If you’re coming fresh into this, you won’t be missing anything… but that is my explaination for the pictures containing wrapping text.
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.

The second was that the selected item background makes your eyes bleed such a horrid blue color you’ll feel like Paul Atreides staring at a stone burner.
Was that a little too geek? My apologies.
Read the rest of this entry »
February 7th, 2008
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’re going to make it work.
First of all, if you’re going to test your comboBox design, you should have it hooked up to an ItemsSource. Don’t have one? I have a tutorial in which I walk through attaching an RSS feed to your control. It was originally written for the ListView, but it will work fine for a ComboBox.
To start out… this is your standard ComboBox:

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).
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.
First, a little bit about the structure of the comboBox.
Read the rest of this entry »
February 4th, 2008
I’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’ve finally figured it out, so I thought I’d share.
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 post that can help you with that over here. Bind your comboBox to the “Items” part of the New York Times RSS feed.
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’s just no fun.
Starting out, your ComboBox should look something like this:

Right click on your ComboBox and select “Edit Other Templates -> Edit Generated Items (ItemTemplate)-> Create Empty…” Give your new data template a name and Blend will take you into the Data Template design.
Read the rest of this entry »