WPF TabControl和DataTemplates [英] WPF TabControl and DataTemplates

查看:160
本文介绍了WPF TabControl和DataTemplates的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组ViewModel,它们绑定到TabControl的ItemsSource属性。我们将这些ViewModel称为AViewModel,BViewModel和CViewModel。其中的每一个都需要具有不同的ItemTemplate(用于标题;因为它们各自需要显示不同的图标)和不同的ContentTemplate(因为它们具有非常不同的交互模型)。

I've got a set of ViewModels that I'm binding to the ItemsSource property of a TabControl. Let's call those ViewModels AViewModel, BViewModel, and CViewModel. Each one of those needs to have a different ItemTemplate (for the header; because they each need to show a different icon) and a different ContentTemplate (because they have very different interaction models).

我想要的是这样的东西:

What I'd like is something like this:

在某处的Resource.xaml文件中定义:

Defined in Resource.xaml files somewhere:

<DataTemplate x:Key="ItemTemplate" DataType="{x:Type AViewModel}">
    ...
</DataTemplate>

<DataTemplate x:Key="ItemTemplate" DataType="{x:Type BViewModel}">
    ...
</DataTemplate>

<DataTemplate x:Key="ItemTemplate" DataType="{x:Type CViewModel}">
    ...
</DataTemplate>

<DataTemplate x:Key="ContentTemplate" DataType="{x:Type AViewModel}">
    ...
</DataTemplate>

<DataTemplate x:Key="ContentTemplate" DataType="{x:Type BViewModel}">
    ...
</DataTemplate>

<DataTemplate x:Key="ContentTemplate" DataType="{x:Type CViewModel}">
    ...
</DataTemplate>

单独定义:

<TabControl ItemTemplate="[ Some way to select "ItemTemplate" based on the type ]"
            ContentTemplate="[ Some way to select "ContentTemplate" based on the type ]"/>

现在,我知道,实际上,每次我使用相同的键定义一个DataTemplate时,要抱怨。但是,有什么我可以做的类似于使我根据名称和数据类型将DataTemplate放入TabControl的操作吗?

Now, I know that realistically, each time I define a DataTemplate with the same key the system is just going to complain. But, is there something I can do that's similar to this that will let me put a DataTemplate into a TabControl based on a name and a DataType?

推荐答案

一种方法是使用 DataTemplateSelector s并让每个人从单独的 ResourceDictionary 解析资源

One way would be to use DataTemplateSelectors and have each one resolve the resource from a separate ResourceDictionary.

这篇关于WPF TabControl和DataTemplates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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