A third solution:
This is slightly more work up-front, better is easier in the long-run if you're binding to loads of Enums. Use a Converter which takes the enumeration's type as a paramter, and converts it to an array of strings as an output.
In VB.NET:
Public Class EnumToNamesConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Return [Enum].GetNames(DirectCast(value, Type))
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
Or in C#:
public sealed class EnumToNamesConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Enum.GetNames(value.GetType());
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw New NotSupportedException()
}
}
Then in your Application.xaml
, add a global resource to access this converter:
<local:EnumToNamesConverter x:Key="EnumToNamesConverter" />
Finally use the converter in any XAML pages where you need the values of any Enum...
<ComboBox ItemsSource="{Binding
Source={x:Type local:CompassHeading},
Converter={StaticResource EnumToNamesConverter}}" />