-
-
Save thomaslevesque/3926252bda9c8a485c6e to your computer and use it in GitHub Desktop.
Markup extension that returns the values of an enum type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EnumValuesExtension : MarkupExtension | |
{ | |
public EnumValuesExtension() | |
{ | |
} | |
public EnumValuesExtension(Type enumType) | |
{ | |
EnumType = null; | |
} | |
public override object ProvideValue(IServiceProvider serviceProvider) | |
{ | |
if (EnumType == null) | |
throw new InvalidOperationException("EnumType was not specified"); | |
object[] values = Enum.GetValues(EnumType).Cast<object>().ToArray(); | |
if (InsertNull == NullPosition.Before) | |
values = new object[] { null }.Concat(values).ToArray(); | |
else if (InsertNull == NullPosition.After) | |
values = values.Concat(new object[] { null }).ToArray(); | |
return values; | |
} | |
[ConstructorArgument("enumType")] | |
public Type EnumType { get; set; } | |
public NullPosition InsertNull { get; set; } | |
} | |
public enum NullPosition | |
{ | |
None, | |
Before, | |
After | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ComboBox ItemsSource="{my:EnumValues my:MyEnum, InsertNull=Before}"> | |
... | |
</ComboBox> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment