65.9K
CodeProject is changing. Read more.
Home

Silverlight combobox without items source with a bound selected value

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Sep 8, 2011

CPOL
viewsIcon

13559

A simple combo box that will have predefined items but the selected value bound to a property from our View Model.

Sometimes we just need a simple combo box that will have predefined items but the selected value must be bound to a property from our View Model. The XAML below is a simple approach to resolve the task:

<combobox>
    Width="220"
    SelectedValue="{Binding Path=Employee.Gender, Mode=TwoWay}"
    SelectedValuePath="Content">
 <combobox.items>
  <comboboxitem content=" " />
  <comboboxitem content="Male" />
  <comboboxitem content="Female" />
 </combobox.items>
</combobox>

The trick is to set the SelectedValuePath to the Content and bind the SelectedValue.