Convert Enum to String / String to Enum
October 15th, 2009
1 comment
I found this today. I need to convert an enumeration into a string list to populate a combobox. I had been looking at converters to do the heavy lifting, but realized that I should do the work in the ViewModel. That simplifies the XAML a bit. While I was typing out code to read the enum values from the settings file Resharper showed me a choice… I could use my custom converter or use the one in the Component Model. Well, I’d rather use the framework solution over a custom solution any day. If nothing else it’s less to test.
Here’s an example from MSDN:
using System.ComponentModel; Enum myServer= Servers.Exchange; string myServerString = "BizTalk"; Console.WriteLine(TypeDescriptor.GetConverter(myServer).ConvertTo(myServer, typeof(string))); Console.WriteLine(TypeDescriptor.GetConverter(myServer).ConvertFrom(myServerString));
Enjoy,
References:
MSDN EnumConverter Class
http://msdn.microsoft.com/en-us/library/system.componentmodel.enumconverter.aspx
C#, Converters, Enums, MSDN, WPF
