TabControl 未找到 TabControl 项的数据模板 [英] TabControl not finding data templates for TabControl items

查看:21
本文介绍了TabControl 未找到 TabControl 项的数据模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一组 TabViewModel 项绑定到一个 TabControl.每个都有一个标题 string 属性和一个我自己的自定义类型 BaseTabContentViewModel 的内容属性,这是每个实际选项卡数据视图模型实现的抽象类.例如 ValuationTabViewModel,它是 BaseTabContentViewModel 的子类.

I am binding a collection of TabViewModel items to a TabControl. Each of these has a header string property, and a content property of my own custom type BaseTabContentViewModel, an abstract class which each actual tab data viewmodel implements. Eg ValuationTabViewModel which is a sub-class of BaseTabContentViewModel.

我将新的 TabViewModel 添加到 Observable 以供 TabControl 选取并显示在 UI 中.我已经覆盖了选项卡控件和标题布局的样式模板,它们工作正常.唯一的问题是内容没有根据类型在我的资源字典中找到模板,它只是显示视图模型的完全限定类名,表明它没有找到该类的默认模板.

I add the new TabViewModel to the Observable<TabViewModel> for the TabControl to pick up and it shows in the UI. I have overridden style templates for the layout of the tab control and header which work fine. The only trouble is the content doesn't find the template in my resource dictionary based on its type, it just displays the full qualified class name of the viewmodel, showing that it is not finding a default template for this class.

为什么不显示正在显示的 ValuationTabViewModel,在下面找到此类型的数据模板?

Why isn't the ValuationTabViewModel that is being displayed, finding the datatemplate for this type below?

我的主视图模型.

public ObservableCollection<TabViewModel> DetailTabs { get; }

var valuationTab = new TabViewModel(DetailTabConstants.ValuationTab, new ValuationTabViewModel(_eventAggregator, _errorNotifier, _windsorContainer));

DetailTabs = new ObservableCollection<TabViewModel> { valuationTab };

主 XAML

                            <TabControl Margin="0,-2,0,0" x:Name="SelectionTabs" Style="{StaticResource DetailTabControl}" ItemsSource="{Binding DetailTabs}" 
                                        SelectedValue="{Binding SelectedTab, Mode=TwoWay}" ItemContainerStyle="{StaticResource DetailTabItem}">
                                <TabControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Header}" />
                                    </DataTemplate>
                                </TabControl.ItemTemplate>
                                <TabControl.ContentTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Content}" />
                                    </DataTemplate>
                                </TabControl.ContentTemplate>
                            </TabControl>

我希望它使用的内容样式模板

The content style template I want it to use

<DataTemplate x:Key="ValuationTabTemplate" DataType="{x:Type detailTabs1:ValuationTabViewModel}" >
        <detailTabs:ValuationTab Margin="0,10,0,10" />
    </DataTemplate>

还有我的 Tab 项 ViewModel 类

And my Tab item ViewModel class

public class TabViewModel : ViewModelBase
{
    private string _header;
    private BaseTabContentViewModel _content;

public string Header
{
    get => _header;
    set
    {
        _header = value; 
        RaisePropertyChanged(nameof(Header));
    }
}

public BaseTabContentViewModel Content
{
    get => _content;
    set
    {
        _content = value;
        RaisePropertyChanged(nameof(Content));
    }
}

public TabViewModel(string header, BaseTabContentViewModel viewModel)
{
    Header = header;
    Content = viewModel;            
}
}

推荐答案

移除 元素并定义一个 隐式 DataTemplate每种类型的代码>(没有x:Key):

Remove the <TabControl.ContentTemplate> element and define an implicit DataTemplate (without an x:Key) for each type:

<TabControl Margin="0,-2,0,0" x:Name="SelectionTabs" Style="{StaticResource DetailTabControl}" ItemsSource="{Binding DetailTabs}" 
            SelectedValue="{Binding SelectedTab, Mode=TwoWay}" ItemContainerStyle="{StaticResource DetailTabItem}">
    <TabControl.Resources>
        <DataTemplate DataType="{x:Type detailTabs1:ValuationTabViewModel}">
            <detailTabs:ValuationTab Margin="0,10,0,10" />
        </DataTemplate>
    </TabControl.Resources>
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Header}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
</TabControl>

这篇关于TabControl 未找到 TabControl 项的数据模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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