Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A solution to your problem is discussed in this <a href="http://www.codeproject.com/KB/codegen/T4Metadata.aspx#_Toc239401270" rel="noreferrer">article</a>.</p> <p>The basic idea is to create a ViewModel MetaData set of static(c#) classes that hold the string value of the properties of your ViewModel classes which you can then use in your xaml. The article explains how to use T4 text generation to create these static metadata classes. You could use any code generation tool of your preference.</p> <p>so your VM has the following:</p> <pre><code>namespace Mine { public class MyViewModel { public int MyInt {get;set;} public string MyString {get;set;} } } </code></pre> <p>And you code generation would create this:</p> <pre><code>namespace Mine.MetaData { public static class MyViewModelMetaData { public const string MyInt = "MyInt"; public const string MyString = "MyString"; } } </code></pre> <p>and then in your xaml you would add the namespace to your xaml and bind your controls to the metadata class</p> <pre><code>&lt;TextBox Text="{Binding Path={x:Static Metadata:MyViewModelMetadata.MyInt}}"/&gt; </code></pre> <p>If you use an add-in like <a href="http://www.jetbrains.com/resharper/" rel="noreferrer">resharper</a> then it will give you intellisense on the properties of the static class and also because you are referencing an exact property in a static class, when the static class gets regenerated your xaml should not compile.</p> <p>It's pretty slick, I think it's awesome and it has the chance of keeping most people sane, but your mileage may vary. :)</p> <p>EDIT:</p> <p>By the way, I don't buy the "ViewModels are tightly coupled to the Views". In my opinion Views are inextricably bound to their ViewModels, but it should only be one way. ViewModels should be completely independent of any view implementation. It's like the ViewModel is the interface and the View is the concrete implemented class. So for this reason I don't put in any WPF-specific properties(e.g. Visibility enumeration) into my ViewModel because that binds me to use WPF for eternity(which isn't really a bad thing :) ), but it compromises maintenance.</p>
 

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