Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #48 – How to Implement a Combobox

With the release of Silverlight 2 RC0 there are three new controls we will be discussing that were not available for beta 2. The three new controls with a Tip of the Day for each include:

For this tip we will be exploring the ComboBox control.

The following code below shows how to declare and size a combo box in your XAML:

<ComboBox Width="100">
    <ComboBox.Items >
        <ComboBoxItem Content="1st Item"></ComboBoxItem>
        <ComboBoxItem Content="2nd Item"></ComboBoxItem>
        <ComboBoxItem Content="3rd Item"></ComboBoxItem>
    </ComboBox.Items>
</ComboBox>

If you were to run this code you would see a combo box without any selected items:

image

Click the drop down button and you will see your three items displayed:

image

If you want to set a default selection for your combo box add the property IsSelected=”true” to any of your ComboBoxItems:

<ComboBox Width="100">
    <ComboBox.Items >
        <ComboBoxItem Content="1st Item"></ComboBoxItem>
        <ComboBoxItem Content="2nd Item"></ComboBoxItem>
        <ComboBoxItem IsSelected="True" Content="3rd Item"></ComboBoxItem>
    </ComboBox.Items>
</ComboBox>

If you run this code, you will see the 3rd item selected by default:

image

The ComboBox control also supports data binding. The following code below is an example implementation.

Few notes about this code:

  • By setting the DisplayMemberPath property you can specify which data item in your data you want displayed in the ComboBox. In our case below we set this to be the Name. We could have also set DisplayMemberPath =”Price” which would than show the car prices in the ComboBox.
  • Setting the SelectedIndex allows you to specify which item in the ComboBox you want selected.
  • I have attached an event to detect when the selection in the ComboBox has changed. The SelectedItem member of our ComboBox will return us the car that is selected at any given time.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
 
namespace Password
{
    public partial class Page : UserControl
    {
        ComboBox _cars = new ComboBox();
 
        public Page()
        {
            InitializeComponent();
 
            List<Car> carList = new List<Car>();
 
            carList.Add(new Car() { Name = "Ferrari", Price = 150000 });
            carList.Add(new Car() { Name = "Honda", Price = 12500 });
            carList.Add(new Car() { Name = "Toyota", Price = 11500 });
 
            ComboBox cb = new ComboBox();
            _cars.DisplayMemberPath = "Name";
            _cars.ItemsSource = carList;
            _cars.SelectedIndex = 2;
            _cars.SelectionChanged += new SelectionChangedEventHandler(cb_SelectionChanged);
 
            MyCanvas.Children.Add(_cars);
        }
 
        void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Car selectedCar = (Car) _cars.SelectedItem;
 
            int price = selectedCar.Price;
            string carName = selectedCar.Name;
        }
    }
 
    public class Car
    {
        public string Name { get; set; }
        public int Price { get; set; }
    }
}

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

With the release of Silverlight 2 RC0 there are three new controls we will be discussing that were not

# September 29, 2008 3:39 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

With the release of Silverlight 2 RC0 there are three new controls we will be discussing that were not

# September 29, 2008 5:43 PM

Silverlight Tip of the Day #48 ??? How to Implement a Combobox - Silverlight Tips of the Day - Blog by Mike Snow said:

Pingback from  Silverlight Tip of the Day #48 ??? How to Implement a Combobox - Silverlight Tips of the Day - Blog by Mike Snow

# September 29, 2008 5:44 PM

Silverlight Tip of the Day #47 ??? How to Implement a Password Box - Silverlight Tips of the Day - Blog by Mike Snow said:

Pingback from  Silverlight Tip of the Day #47 ??? How to Implement a Password Box - Silverlight Tips of the Day - Blog by Mike Snow

# September 29, 2008 5:44 PM

2008 September 30 - Links for today « My (almost) Daily Links said:

Pingback from  2008 September 30 - Links for today &laquo; My (almost) Daily Links

# September 30, 2008 3:52 AM

Tregarick said:

Have been trying to use a combo box which changes based on the 'SelectionChanged' event of a datagrid.  Seemed to work with no problems which is great as this is a pretty common scenario for combo boxes.  However if I change the value in the combo box and then click on another row in my grid I get a 'Value does not fall within the expected range error' and my whole silverlight app whites out.  I have seen many other posts with this issue and was wondering if it was a known issue or something I have done wrong?  If its known is their any fix estimate or work around as its a pretty glaring bug that shouldn't have made it past testing.

# September 30, 2008 8:18 AM

Silverlight news for September 30, 2008 said:

Pingback from  Silverlight news for September 30, 2008

# September 30, 2008 9:45 AM

mike.snow said:

Hi Tregarick, I have not yet worked with datagrids.

I would recommend posting your question here: silverlight.net/.../35.aspx

I will try to get to datagrids soon, it's on my radar.

Thanks!

# September 30, 2008 12:11 PM

Dew Drop – September 30, 2008 (Evening Edition) | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop &ndash; September 30, 2008 (Evening Edition) | Alvin Ashcraft's Morning Dew

# September 30, 2008 10:28 PM

Community Blogs said:

In this post: Mike Snow, Bill Reiss, Jesse Liberty, Harsh Bardhan, Tim Heuer, Matthias Shapiro, and Jeff

# October 1, 2008 9:53 AM

Silverlight 2 RC0 - ComboBox at Blog von J??rgen Ebner said:

Pingback from  Silverlight 2 RC0 - ComboBox at Blog von J??rgen Ebner

# October 2, 2008 4:48 AM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #57 Title: How to Dynamically Load a Silverlight Control within another Silverlight

# October 8, 2008 11:06 PM

MS Tech News » Silverlight Tips of the Day ??? Week 8 said:

Pingback from  MS Tech News &raquo; Silverlight Tips of the Day ??? Week 8

# October 27, 2008 3:27 PM

MS Tech News » Silverlight Tips of the Day ??? Week 8 said:

Pingback from  MS Tech News &raquo; Silverlight Tips of the Day ??? Week 8

# October 27, 2008 3:33 PM