Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting Window.Content to ViewModel - simple data template not working
    text
    copied!<p>Trying to get my head around MVVM, and getting a simple window to render its viewmodel as a view via data templating.</p> <p>in App.xaml:</p> <pre><code>&lt;Application.Resources&gt; &lt;DataTemplate DataType="{x:Type vm:TestViewModel}"&gt; &lt;vw:TestView /&gt; &lt;/DataTemplate&gt; &lt;/Application.Resources&gt; </code></pre> <p>View definition:</p> <pre><code>&lt;UserControl x:Class="MyNamespace.TestView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"&gt; &lt;Grid&gt; &lt;TextBlock&gt;TESTING&lt;/TextBlock&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>In MainWindowViewModel:</p> <pre><code>private void onOpenTestView(object sender) { Window w = new Window(); w.Content = new TestViewModel(); w.Show(); } </code></pre> <p>Running the app results in a window with a "MyNamespace.TestViewModel" string, instead of "TESTING", which would infer my data template is not being found.</p> <p>I am very new to all this so am I missing something obvious? I don't think it's a string matching issue since if I deliberately misspell the view or model in the XAML then it doesn't compile.</p> <p>Should my new window be able to access my app resources (and thus my datatemplate) ok?</p> <p>Cheers, Jeremy</p> <p>EDIT: FIXED (Can't answer for 8 hours)</p> <p>Due to MS Bug where resources not being read unless at least one style is set.</p> <p>See <a href="http://connect.microsoft.com/VisualStudio/feedback/details/553528/defaultstylekey-style-not-found-in-inner-mergeddictionaries" rel="nofollow">http://connect.microsoft.com/VisualStudio/feedback/details/553528/defaultstylekey-style-not-found-in-inner-mergeddictionaries</a></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