<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Convert Enum to String / String to Enum</title>
	<atom:link href="http://digitalMoto.net/blog/2009/10/15/convert-enum-to-string-string-to-enum/feed/" rel="self" type="application/rss+xml" />
	<link>http://digitalMoto.net/blog/2009/10/15/convert-enum-to-string-string-to-enum/</link>
	<description>Off-idle banter on motorcycling, software development and life in the Pacific Northwest.</description>
	<lastBuildDate>Mon, 13 Jun 2011 18:46:25 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: digital</title>
		<link>http://digitalMoto.net/blog/2009/10/15/convert-enum-to-string-string-to-enum/comment-page-1/#comment-8516</link>
		<dc:creator>digital</dc:creator>
		<pubDate>Fri, 06 Nov 2009 21:16:17 +0000</pubDate>
		<guid isPermaLink="false">http://digitalMoto.net/blog/?p=919#comment-8516</guid>
		<description>Having learned more, I realized I was working way too hard.  The enumeration can and will be converted into a usable object at run time.  Just wire it up.

View Model Code:
&lt;pre&gt;
public Media Media { get; set; }
public List&lt;media&gt; MediaList { get { return Enum.GetValues(typeof (Media)).Cast&lt;/media&gt;&lt;media&gt;().ToList(); } }
&lt;/pre&gt;

XAML Code:
&lt;pre&gt; 
&lt;ComboBox 
  x:Name=&quot;cbxMedia&quot; 
  ItemsSource=&quot;{Binding MediaList}&quot; 
  SelectedItem=&quot;{Binding Media}&quot; 
  IsSynchronizedWithCurrentItem=&quot;True&quot; 
/&gt;&lt;/pre&gt;

When the user changes selections on the screen, the background value is automatically converted to the correct enum value.  I had been thinking that everything had to be converted in to and out of string based objects.  Nope.  That is not to say that I am using a best practice.  I&#039;m still gaining experience with WPF and the M-V-VM pattern.  

Of course, the other way to do this would be to express the enumeration as static resource in the XAML.  I looked at it.  I got it working, then decided that I was breaking too many rules because the View would have to know about the underlying business object.  The whole point in M-V-VM is to create ignorant Views.  The data context is the only thing the View should know.  So no embedded info on business layer objects.  

This works and it is simpler.  Simpler is always better, but not too simple.  To paraphrase Einstein.  

</description>
		<content:encoded><![CDATA[<p>Having learned more, I realized I was working way too hard.  The enumeration can and will be converted into a usable object at run time.  Just wire it up.</p>
<p>View Model Code:</p>
<pre>
public Media Media { get; set; }
public List&lt;media&gt; MediaList { get { return Enum.GetValues(typeof (Media)).Cast&lt;/media&gt;&lt;media&gt;().ToList(); } }
</pre>
<p>XAML Code:</p>
<pre>
&lt;ComboBox
  x:Name="cbxMedia"
  ItemsSource="{Binding MediaList}"
  SelectedItem="{Binding Media}"
  IsSynchronizedWithCurrentItem="True"
/&gt;</pre>
<p>When the user changes selections on the screen, the background value is automatically converted to the correct enum value.  I had been thinking that everything had to be converted in to and out of string based objects.  Nope.  That is not to say that I am using a best practice.  I&#8217;m still gaining experience with WPF and the M-V-VM pattern.  </p>
<p>Of course, the other way to do this would be to express the enumeration as static resource in the XAML.  I looked at it.  I got it working, then decided that I was breaking too many rules because the View would have to know about the underlying business object.  The whole point in M-V-VM is to create ignorant Views.  The data context is the only thing the View should know.  So no embedded info on business layer objects.  </p>
<p>This works and it is simpler.  Simpler is always better, but not too simple.  To paraphrase Einstein.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

