Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF MVVM: Convention over Configuration for ResourceDictionary?
    text
    copied!<hr> <p><b>Update</b></p> <p>In the wiki spirit of StackOverflow, here's an update: </p> <p>I spiked Joe White's IValueConverter suggestion below. It works like a charm. </p> <p>I've written a "quickstart" example of this that automates the mapping of ViewModels->Views using some cheap string replacement. If no View is found to represent the ViewModel, it defaults to an "Under Construction" page. I'm dubbing this approach "WPF MVVM White" since it was Joe White's idea. Here are a couple screenshots.</p> <p>The first image is a case of "[SomeControlName]ViewModel" has a corresponding "[SomeControlName]View", based on <b>pure naming convention</b>. The second is a case where the ModelView doesn't have any views to represent it. No more ResourceDictionaries with long ViewModel to View mappings. It's pure naming convention now.</p> <p><img src="https://i.stack.imgur.com/vnZOm.jpg" alt="Wpf Mvvm White"></p> <p>I posted a download of the project here: <a href="https://dl.dropbox.com/u/19646117/Mvvm.White.Quickstart.zip" rel="nofollow noreferrer">Mvvm.White.Quickstart.zip</a></p> <hr> <p><b>Original Post</b></p> <p>I read Josh Smith's <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="nofollow noreferrer">fantastic MSDN article</a> on WPF MVVM over the weekend. It's destined to be a cult classic.</p> <p>It took me a while to wrap my head around the magic of asking WPF to render the <b>ViewModel</b>. </p> <p>It's like saying "Here's a class, WPF. Go figure out which UI to use to present it."</p> <p>For those who missed this magic, WPF can do this by looking up the <b>View</b> for <b>ModelView</b> in the ResourceDictionary mapping and pulling out the corresponding <b>View</b>. (Scroll down to <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="nofollow noreferrer">Figure 10 Supplying a View </a>). </p> <p>The first thing that jumps out at me immediately is that there's already a strong naming convention of:</p> <pre><code>classNameView ("View" suffix) classNameViewModel ("ViewModel" suffix) </code></pre> <p>My question is:</p> <p>Since the <a href="http://msdn.microsoft.com/en-us/library/system.windows.resourcedictionary.aspx" rel="nofollow noreferrer">ResourceDictionary</a> can be manipulated programatically, I"m wondering if anyone has managed to Regex.Replace the whole thing away, <b>so the lookup is automatic</b>, and any new View/ViewModels get resolved by virtue of their naming convention?</p> <p>[Edit] What I'm imagining is a hook/interception into ResourceDictionary. </p> <p>... Also considering a method at startup that uses interop to pull out <code>*View$</code> and <code>*ViewModel$</code> class names to build the DataTemplate dictionary in code:</p> <pre><code>//build list foreach .... String.Format("&lt;DataTemplate DataType=\"{x:Type vm:{0} }\"&gt;&lt;v:{1} /&gt;&lt;/DataTemplate&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