如何将 DataTemplate 添加到资源中? [英] How to add DataTemplate into resources?

查看:25
本文介绍了如何将 DataTemplate 添加到资源中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类向导,它创建一个向导,其中包含在同一项目中定义的页面.PageViewModel 与 PageView 分离.PageViewModel 是一个普通的 C# 类,派生自 PageViewModelBase 抽象类,PageView 是一个 UserControl.为了定义 PageViewModel 和 PageView 之间的映射,我为项目中的每个页面编写了以下代码:

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>

现在我想在用户的代码调用我的向导的构造函数时向向导添加页面.意思是把Pages View和ViewModel移到用户端.例如,为了创建一个单页向导,用户将编写以下代码:向导 usersWizard = new Wizard(new usersViewModel(), new userView());问题是我不知道如何在我的构造函数中提供 viewModel 和 View 之间的映射.据我所知,我可以使用两种不同的方法来解决这个问题.首先,要使用 FrameworkElementFactory 但以下代码不起作用:

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);

第二,使用XamlReader.我可以使用它创建数据模板,但我不知道如何将其附加到资源中.

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

推荐答案

在您创建 DataTemplate 的代码隐藏中,有几个错误:

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

  1. "dt.DataType = wpfView.GetType()" 应该是 "dt.DataType = wpfviewmodel.GetType()"
  2. 在将模板添加到 ResourceDictionary 时,您应该使用 DataTemplateKey.
  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);

这篇关于如何将 DataTemplate 添加到资源中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆