Mishelle
Mishelle

Reputation: 392

How to use nested views that correspond to my nested viewmodels?

I'm writing a simple Windows Store app, and trying to use MVVM. I have an ItemViewModel which contains a bunch of properties, and an ItemView.xaml which displays it. I have a MainViewModel which contains an ItemViewModel property called CurrentItem. I have a MainView.xaml to display the MainViewModel, but I'm not sure how to use the ItemView.xaml within this.

I've found that I can include it as a resource like this:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="local:ItemView.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>

Then I can use an instance of it like this:

    <StackPanel>
        ...other stuff using other properties of MainVM...
        <local:ItemView></local:ItemView>
    </StackPanel>

But I can't figure out how to bind this ItemView to the CurrentItem property. How do I do that? Or is there a better way to structure it?

Edit

Binding to DataContext as suggested worked in Design mode. But when I run it, the main page will not load, and I get this error:

A first chance exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in Test.exe

WinRT information: Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source'.

The main page loads fine until I put that resource declaration, and the inner page loads fine if I set it as the starting page. So I still question whether my approach is correct. Can I declare one standalone page as a resource in another page? Or does the inner one need to be a UserControl or a template or something else? And can I convert the page, or do I need to rebuild it?

Upvotes: 0

Views: 880

Answers (1)

Carbine
Carbine

Reputation: 7903

You are just creating an instance of the <ItemView/> in the View, I don't see you binding the ViewModel Property CurrentItem to <ItemView/>. Something like

 <local:ItemView DataContext='{Binding CurrentItem}'></local:ItemView>

May be the binding structure may change based on how you have bound the MainView with the MainViewModel. But it should just have the binding done to the correct viewmodel property of ItemView.

Edit

I missed out the other part of your question. Whether you can use your usercontrol(in this case ItemView) as a resource dictionary item. The answer is No. Reference - https://msdn.microsoft.com/en-us/library/hh968442.aspx

A ResourceDictionary and Windows Runtime XAML in general supports these objects for shareable usage:

  • Styles and templates (Style and classes derived from FrameworkTemplate)
  • Brushes and colors (classes derived from Brush, and Color values)
  • Animation types including Storyboard
  • Transforms (classes derived from GeneralTransform)
  • Matrix and Matrix3D
  • Point values
  • Certain other UI-related structures such as Thickness and CornerRadius
  • XAML intrinsic data types

So how should you access in the MainWindow then? You should include it in your namespace something like - xmlns:ItemView="clr-namespace:assemblyname.ItemView.xaml"

Upvotes: 1

Related Questions