I've got the enum like so.
enum Beep {A, B, C }
Now, I wish to populate my combo box with these values as follows (the idea is to follow other's example ).
<ComboBox x:Name="comboBox1"
...
ItemsSource="Binding Source={StaticResource Beep}" />
However, the binding gets a little bit too static and gives me literally the exact string I'm putting in. What did I do wrong and how do I resolve it?
I've also tried following the hint I got to add something like this. To no avail, though.
public List<Beep> Beepies
{
get
{
return new List<Beep>{ Beep.A }
}
}
What more can be done about it? I can get the values into the box if I bind in the code behind by the below. But that's not the point - I wish to XAMLize the approach.
comboBox1.ItemsSource = Enum.GetValues(typeof(Beep));
You have to bind using a collection. Try creating a List<Beep>
.
OR
Create a converter that enumerates the enum
:
public class EnumConverter : IValueConverter
{
public object Convert(object value, ...)
{
return Enum.GetValues(value.GetType());
}
}
You can then use this converter in the binding to XAMLise the approach:
<!-- Put this in resources somewhere -->
<Converters:EnumConverter x:Key="MyEnumConverter"/>
<!-- Change your binding to include the new converter -->
<ComboBox x:Name="comboBox1"
...
ItemsSource="Binding Source={StaticResource Beep},
Converter={StaticResource MyEnumConverter}" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.