Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A third solution:</p> <p>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.</p> <p>In VB.NET:</p> <pre><code>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 </code></pre> <p>Or in C#:</p> <pre><code>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() } } </code></pre> <p>Then in your <code>Application.xaml</code>, add a global resource to access this converter:</p> <pre><code>&lt;local:EnumToNamesConverter x:Key="EnumToNamesConverter" /&gt; </code></pre> <p>Finally use the converter in any XAML pages where you need the values of any Enum...</p> <pre><code>&lt;ComboBox ItemsSource="{Binding Source={x:Type local:CompassHeading}, Converter={StaticResource EnumToNamesConverter}}" /&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload