WPF:具有多个DataTemplates的TabControl [英] WPF: TabControl with multiple DataTemplates

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

问题描述

我有一个 TabControl 和多个 DataTemplate .第一个 DataTemplate 用于搜索,第二个用于显示从该搜索获得的项目.我的 XAML 代码如下:

I have a TabControl with multiple DataTemplate. the first DataTemplate will be used for search reasons and the second will be for displaying items obtained from that search. My XAML code will be as follows:

 <UserControl.Resources>
    <!--First template-->
    <DataTemplate>
        <!-- I will have a DataGrid here-->
    </DataTemplate>

    <!--Second template-->
    <DataTemplate >
         <!-- I will have details of one item of the DataGrid-->
    </DataTemplate>
</UserControl.Resources>


<TabControl ItemsSource="{Binding }"/>

我要完成的是,在 TabControl 中,第一个选项卡将包含第一个 DataTemplate (搜索模板),并且当我双击我的一行时 DataGrid ,将添加带有该行详细信息的标签(换句话说,是带有第二个模板的标签).

What I want to accomplish is that in the TabControl the first tab will contain the first DataTemplate (the search template) and when I double click on one row of my DataGrid, a tab will be added with the details of that row (in other words a tab with the second template).

由于我使用的是 MVVM ,因此我想到了创建两个 UserControl ,每个模板一个,然后捕获双击事件,但是在此之后,我不再知道如何添加标签,因为现在我的搜索模板是一个 UserControl ,它与包含 TabControl 的模板分开.

Since I am using MVVM, I thought of creating two UserControls, one for each template and then catch the double click event, but after this I don't know how to add a tab since now my search template is a UserControl seperated from the one that contains the TabControl.

那我该怎么做?

更新:

UPDATE:

当我阅读答案时,我认为我在陈述问题时并不太清楚.我的问题是如何通过捕获第一个模板的双击事件来添加带有第二个模板的选项卡.独立添加两个模板没有任何问题.

As I read the answers I think I wasn't very clear in stating the problem. My problem is how to add tabs with the second template, by catching double click events from the first template. I don't have any problem in adding the two templates independently.

推荐答案

宁可创建两个 UserControl ,也可以创建并使用 DataTemplateSelector 来切换其他 DataTemplate s.

Rather can creating two UserControls, you can create and use a DataTemplateSelector in order to switch different DataTemplates in.

基本上,从 DataTemplateSelector 创建一个新类,并覆盖 SelecteTemplate 方法.然后在XAML中声明它的一个实例(非常类似于值转换器),然后将其应用于 TabControl ContentTemplateSelector 属性.

Basically, create a new class that inhereits from DataTemplateSelector and override the SelecteTemplate method. Then declare an instance of it in the XAML (much like a value converter), and then apply it to ContentTemplateSelector property of the TabControl.

更多信息可以在这里找到.

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

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