Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add DataTemplate into resources?

Tags:

mvvm

wpf

I have a class Wizard which creates a wizard with pages defined in the same project. The PageViewModel is separated from PageView. PageViewModel is an ordinary C# class derived from PageViewModelBase abstract class and PageView is a UserControl. In order to define a mapping between PageViewModel and PageView I wrote the following code for every page in my project:

 <Window.Resources>
    <DataTemplate DataType="{x:Type OurNewPageViewModel}">
      <OurNewPageView />
    </DataTemplate>
  </Window.Resources>

Now I want to add pages to wizard when the users’ code calls my Wizard’s constructor. It means to move Pages View and ViewModel to the user side. For instance, in order to create a wizard with one page user will write the following code: Wizard usersWizard = new Wizard(new usersViewModel(), new userView()); The problem is I don’t know how to provide the mapping between viewModel and View in my constructor. As far as I understand I can use two different approaches to solve this problem. First, to use FrameworkElementFactory but the following code doesn’t work:

        //let we have WelcomePageView wpview and WelcomePageViewModel wpviewmodel

        FrameworkElementFactory fef = new FrameworkElementFactory(wpview.GetType());
        DataTemplate dt = new DataTemplate();
        dt.DataType = wpview.GetType();

        dt.VisualTree = fef;

        base.Resources.Add(wpviewmodel.GetType(), dt);

Second, to use XamlReader. I can create a data template using it but I don’t know how to attach it to resources.

like image 756
Kirill Lykov Avatar asked Aug 19 '10 06:08

Kirill Lykov


People also ask

What is difference between ItemTemplate and DataTemplate?

You use the ItemTemplate to specify the visualization of the data objects. If your ItemsControl is bound to a collection object and you do not provide specific display instructions using a DataTemplate, the resulting UI of each item is a string representation of each object in the underlying collection.

What is difference between a ControlTemplate and a DataTemplate in WPF?

A ControlTemplate will generally only contain TemplateBinding expressions, binding back to the properties on the control itself, while a DataTemplate will contain standard Binding expressions, binding to the properties of its DataContext (the business/domain object or view model).

What is DataTemplate?

A data template can contain elements that are each bound to a data property along with additional markup that describes layout, color and other appearance. DataTemplate is, basically, used to specify the appearance of data displayed by a control not the appearance of the control itself.

What is WPF item template?

WPF provides various templates for customization of controls. WPF ListView ItemTemplate is in one of these. ItemTemplate of ListView is useful when we have to change the visual presentation of bound data objects.


1 Answers

In your code-behind to create the DataTemplate, there are a couple of errors:

  1. "dt.DataType = wpfView.GetType()" should be "dt.DataType = wpfviewmodel.GetType()"
  2. You should use a DataTemplateKey when adding the template to the ResourceDictionary.

So your code should be something like:

DataTemplate dt = new DataTemplate();
dt.DataType = typeof(PageViewModel);
FrameworkElementFactory fef = new FrameworkElementFactory(typeof(PageView));
dt.VisualTree = fef;
DataTemplateKey dtKey = new DataTemplateKey(typeof(PageViewModel));
this.Resources.Add(dtKey, dt);
like image 105
ASanch Avatar answered Sep 20 '22 17:09

ASanch