0

I'm trying to get into MVVM from a UWP perspective. Everything seems to work almost like on WPF, but I can't get a good ViewModel/View Mapping running. According to the Microsodt-Specialist: https://channel9.msdn.com/Events/Build/2015/3-635 , starts arround 20 Minutes, there is some new special Syntax for the ResourceDictionaries. I've done all steps, which loads the Dictionary as expected, but the straightforward-mapping doesn't work:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="using:ARGUSnet.RaspberryPiFramework.Designer.Views"
xmlns:vm="using:ARGUSnet.RaspberryPiFramework.Designer.ViewModels"
xmlns:local="using:ARGUSnet.RaspberryPiFramework.Designer.Dictionaries"
    x:Class="ARGUSnet.RaspberryPiFramework.Designer.Dictionaries.ViewModelMappingDict">

<DataTemplate x:Key="dtTest" x:DataType="vm:TestViewModel">
    <v:TestView />
</DataTemplate>

</ResourceDictionary>

I think the problem herein lies in the fact, I have to define a Key. Although I found some explanations about this, I don't get why this has to be done nowadays. Anyways, the mapping works, if I do this in the ViewContainer directly:

<Page.Resources>
    <DataTemplate x:DataType="vm:TestViewModel">
        <v:TestView />
    </DataTemplate>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ContentControl 
        Content="{Binding CurrentContent}" />
</Grid>

The problem is, all examples are found are working kinda directly, without an explicit Mapping-Dictionary, or work with Freamworks like mvvmlight. Since this is more for learning purposes, I'd rather make this work without blackboxing the Binding-Mechanics into an Framework.

Is it possible, this is just a Bug in the ResourceDictionary? I also found this Thread, How to associate view with viewmodel or multiple DataTemplates for ViewModel? , but according to my understanding, the question there was about an runtime mapping, while I can bind directly.

Community
  • 1
  • 1
Matthias Müller
  • 3,336
  • 3
  • 33
  • 65
  • Did you create the datatemplate codebehind? Are you calling it using type? The thing is using x:Bind datatype does binding for you at compile time. Thus a code behind is needed for it to be initialized at compiletime. You also have option of creating a `usercontrol` instead of using resources for which you dont have to use any frameworks. – Jerin Apr 03 '16 at 16:28
  • Not sure what you're meaning. Do I have to bind the ViewModels and Models kindahow not only at the ResourceDictionary but also somewhere at Runtime? I have a Code-Hind of the Dictionary, which also gets called, or do you mean something else? – Matthias Müller Apr 03 '16 at 16:52
  • Since you said _but the straightforward-mapping doesn't work_ I assumed that resources dictionary binding isnt working for you. While using resources you would need to bind to template using your resource name while specifying the `ItemSource` for it to bind data. From OP I am understanding your resourcedictionary method didnt work whereas specifying resource directly as page resource worked – Jerin Apr 03 '16 at 18:24
  • I can not recurrence your said, so please post your code about `TestView`. – Jayden Apr 04 '16 at 06:29

0 Answers0