I have a ComboBox that is filled from the values of an enumeration:
<ComboBox ItemsSource="{utils:Enumerate {x:Type models:MyEnum}}"
SelectedItem="{Binding SelectedEnumValue, Converter={StaticResource EnumToStringConverter}}" />
That uses utils:Enumerate, an extension class, to get the values of the enum
public sealed class EnumerateExtension : MarkupExtension
{
public Type Type { get; set; }
public EnumerateExtension(Type type)
{
Type = type;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
var names = Enum.GetNames(Type);
string[] values = new string[names.Length];
for (int i = 0; i < names.Length; i++)
{
values[i] = StringResources.Get(names[i]); //translating the enum value into a readable text
}
return values;
}
}
That works great and displays all the possible values of an enum in a combobox. It also contains nice readable texts instead of enum values like NotReleased.
But I want the comboxbox to have another value - an empty one. So I can select none of the enum values and deselect previously selected values.
How?
You could set the
ItemsSourceto aCompositeCollectionto which you add anotherstring:Replace
System.Runtimewithmscorlibin the namespace declaration if you target the .NET Framework.Or you could add another
stringto thestring[]returned from yourProvideValuemethod.