<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Displaying Enum Values using Data Binding</title><link>http://blogs.msdn.com/wpfsdk/archive/2007/02/22/displaying-enum-values-using-data-binding.aspx</link><description>Some of the FAQs about data binding are: • How do I bind to a method? • How do I bind between instantiated controls? • How do I bind an ItemsControl to an enum? I put together a quick sample that should answer the above questions: &amp;lt; Window xmlns =</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>WPF ComboBox and C# Enum - The Love Story</title><link>http://blogs.msdn.com/wpfsdk/archive/2007/02/22/displaying-enum-values-using-data-binding.aspx#6738859</link><pubDate>Wed, 12 Dec 2007 00:39:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6738859</guid><dc:creator>Tomer Shamam Essential WPF</dc:creator><description>&lt;p&gt;Q: How can I bind a property of type Enum (of any kind) to a ComboBox or ListBox controls? I bet that&lt;/p&gt;
</description></item><item><title>Handy, BUT....</title><link>http://blogs.msdn.com/wpfsdk/archive/2007/02/22/displaying-enum-values-using-data-binding.aspx#8340214</link><pubDate>Thu, 27 Mar 2008 22:14:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8340214</guid><dc:creator>jrichview</dc:creator><description>&lt;p&gt;Thanks for the example. &amp;nbsp;However, it doesn't really bind to a data field. It's a great example of how to get the list to show the enum values, but it doesn't associate the selected value with anything.&lt;/p&gt;
&lt;p&gt;Additionally, in real world apps nobody would do it this way. &amp;nbsp;You would want a class to associate the enum values with some internationalizable text, not the actual name of the enum value. For example, if your enum value were MyEnum.Column1 you would want to see the user-friendly text &amp;quot;Column One&amp;quot; or at least &amp;quot;Column 1&amp;quot; with a space. &amp;nbsp;And you'd want to be able to change that for other languages.&lt;/p&gt;
&lt;p&gt;Woulda been great if the framework could've provided an easy means to accomodate that considering how common enums are as a property value.&lt;/p&gt;
</description></item><item><title>re: JRich's Handy..</title><link>http://blogs.msdn.com/wpfsdk/archive/2007/02/22/displaying-enum-values-using-data-binding.aspx#9644771</link><pubDate>Wed, 27 May 2009 21:24:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9644771</guid><dc:creator>KellenF</dc:creator><description>&lt;p&gt;A little late to this party but...&lt;/p&gt;
&lt;p&gt;You can get around all the shortcomings you mentioned with a ValueConverter. &amp;nbsp;Simply bind to the Enum.GetValues method, and have your ValueConverter lookup the internationalized pretty print string for the enum, and return a ComboBoxItem with a DisplayProperty of the print string, and a ValueProperty of the enum.&lt;/p&gt;
</description></item><item><title>re: Displaying Enum Values using Data Binding</title><link>http://blogs.msdn.com/wpfsdk/archive/2007/02/22/displaying-enum-values-using-data-binding.aspx#9702400</link><pubDate>Sat, 06 Jun 2009 04:31:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9702400</guid><dc:creator>Thomas Levesque</dc:creator><description>&lt;p&gt;I find that using an ObjectDataProvider for doing that is too verbose... I use a custom markup extension instead :&lt;/p&gt;
&lt;p&gt;[MarkupExtensionReturnType(typeof(IEnumerable))]&lt;/p&gt;
&lt;p&gt;public class EnumValuesExtension : MarkupExtension&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public EnumValuesExtension()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public EnumValuesExtension(Type enumType)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.EnumType = enumType;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[ConstructorArgument(&amp;quot;enumType&amp;quot;)]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public Type EnumType { get; set; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public override object ProvideValue(IServiceProvider serviceProvider)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (this.EnumType == null)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw new ArgumentException(&amp;quot;The enum type is not set&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return Enum.GetValues(this.EnumType);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;It can be used like that :&lt;/p&gt;
&lt;p&gt;&amp;lt;ListBox Name=&amp;quot;myComboBox&amp;quot; SelectedIndex=&amp;quot;0&amp;quot; Margin=&amp;quot;8&amp;quot;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ItemsSource=&amp;quot;{my:EnumValues HorizontalAlignment}&amp;quot;/&amp;gt;&lt;/p&gt;
</description></item></channel></rss>