Click here to Skip to main content
15,886,873 members
Articles / Desktop Programming / XAML
Tip/Trick

Silverlight combobox without items source with a bound selected value

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
8 Sep 2011CPOL 13.2K  
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:


XML
<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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --