Say I have an enum with four values:
public enum CompassHeading { North, South, East, West }
What XAML would be required to have a ComboBox be populated with these items?
<ComboBox ItemsSource="{Binding WhatGoesHere???}" />
Ideally I wouldn't have to set up C# code for this.
You can get or set the combo box's selected item by using the SelectedItem property, and get or set the index of the selected item by using the SelectedIndex property. You populate the ComboBox by adding objects directly to the Items collection or by binding the ItemsSource property to a data source.
On button click event handler, we add the content of TextBox to the ComboBox by calling ComboBox. Items. Add method. Now if you enter text in the TextBox and click Add Item button, it will add contents of the TextBox to the ComboBox.
You can use the ObjectDataProvider to do this:
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="odp"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="local:CompassHeading"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <ComboBox ItemsSource="{Binding Source={StaticResource odp}}" />
I found the solution here:
http://bea.stollnitz.com/blog/?p=28
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With