views:

40

answers:

2

I am trying to add resource dictionary into my silverlight-4 aplication (suggested in "Applying a View to a ViewModel" chapter of the http://msdn.microsoft.com/en-us/magazine/dd419663.aspx article).

The 1st problem: I don't see any resource in my MainPage. Am I correctly understand that I need to add resource dictionary manually into Silverlight app?

The 2nd: When I did that, in the Dictionary1.xaml file

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib">
    <DataTemplate DataType="{x:Type vm:MyViewModel}">
        <vw:MyView />
    </DataTemplate>
</ResourceDictionary>

I am getting an error: Can't resolve a symbol 'DataType'...

Is there any idea hot to do that?

A: 

You also need to add an x:Key into the DataTemplate if it's going to be in the ResourceDictionary.

Michael S. Scherotter
+1  A: 

ad 1: MainPage has a ResourceDictionary. You add elements to it in xaml like this:

<MainPage>
  <MainPage.ResourceDictionary>
    <DataTemplate>
      <vw:MyView />
    </DataTemplate>
  </MainPage.ResourceDictionary>
  ...

You can add a ResourceDictionary to the MainPage.ResourceDictionary by using the Source and MergedDictionaries properties of ResourceDictionary:

<MainPage>
  <MainPage.ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Dictionary1.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </MainPage.ResourceDictionary>
  ...

ad 2: DataTemplate does not have a Property DataType in the Silverlight framework. :-(

Ozan
Thank you for information... Could you please also let me know if there is any possibility to work with resources in Silverlight? Thank you in advance
Budda