代码之家  ›  专栏  ›  技术社区  ›  Edward Tanguay

为什么在XAML选项卡控件的选项卡内容区域中显示选项卡标题?

  •  3
  • Edward Tanguay  · 技术社区  · 16 年前

    我有一个 选项卡控件 其itemssource绑定到 可观察的视图集合 (usercontrols)每个都有作为其根元素的 标签项目 . 但是,当显示时, 页眉 文本在每个tabitem的内容中,就好像 用户控制 包装导致冲突:

    alt text http://i31.tinypic.com/2z7pctz.png

    TabControl在 智能窗体视图.xaml:

    <UserControl x:Class="TestApp.Views.SmartFormView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel
            Margin="10">
            <TextBlock Text="{Binding Title}"
                FontSize="18"/>
            <TextBlock Text="{Binding Description}"
                FontSize="12"/>
    
            <TabControl
                Margin="0 10 0 0"
                ItemsSource="{Binding SmartFormAreaViews}"/>
        </StackPanel>
    </UserControl>
    

    我必须更改什么才能在TabControl中将TabItem显示为TabItem?

    以下是名为 smartformareview.xaml(智能窗体视图.xaml):

    <UserControl x:Class="TestApp.Views.SmartFormAreaView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <TabItem Header="This is the header">
            <StackPanel Margin="10">
                <TextBlock Text="this is the content"/>
            </StackPanel>
        </TabItem>
    </UserControl>
    

    这里是我创建和加载每个视图到 可观测采集 :

    var areas = from area in xmlDoc.Descendants("area")
                select area;
    foreach (var area in areas)
    {
        SmartFormArea smartFormArea = new SmartFormArea();
        smartFormArea.IdCode = area.Attribute("idCode").Value;
        smartFormArea.Title = area.Attribute("title").Value;
        SmartFormAreaPresenter smartFormAreaPresenter = new SmartFormAreaPresenter(smartFormArea);
        SmartFormAreaViews.Add(smartFormAreaPresenter.View as SmartFormAreaView);
    }
    
    2 回复  |  直到 16 年前
        1
  •  4
  •   Phil Devaney    16 年前

    对于任何item s control,如果添加到其item s集合(直接或通过itemssource)的项不是该控件的item容器的实例,则每个项都包装在item容器的实例中。项容器是一个类,如TabItem或ListBoxItem。项目容器通常是ContentControl或HeaderedContentControl,而您的实际项目被分配给其内容属性,因此您可以使用模板等来控制内容的显示方式。您还可以使用itemControl的itemContainerStyle属性设置项容器本身的样式。

    在这种特殊情况下,您应该将itemssource绑定到smartformareapresenter列表。然后对选项卡控件使用类似的方法:

    <TabControl ItemsSource="{Binding SmartFormAreaPresenters}">
      <TabControl.ItemContainerStyle>
        <Style TargetType="{x:Type TabItem}">
          <Setter Property="Header" Value="{Binding HeaderText}" />
        </Style>
      </TabControl.ItemContainerStyle>
    
      <TabControl.ContentTemplate>
        <DataTemplate DataType="{x:Type local:SmartFormAreaPresenter}">
          <local:SmartFormAreaView />
        </DataTemplate>
      </TabControl.ContentTemplate>
    </TabControl>
    

    其中headerText是您的smartformareapresenter上合适的属性。您还应该从smartformareaview定义中删除tabitem。每个视图的DataContext将自动设置为适当的演示者。

    见WPF博士 blog 对于各种与控制相关的主题的精彩讨论。

        2
  •  0
  •   Fabrício Matté    16 年前

    这个 TabControl 只有当控件可以强制转换为 TabItem ,而不是用户控件或smartformeareview等。

    所以你要么用普通的 TabItems 使用可视化树或子类 表项 或者您将 选项卡控件 覆盖它的 IsItemItsOwnContainerOverride 方法,接受您的类型作为容器。

    方法应如下所示:

    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return item is YourControlTypeHere || item is TabItem;
    }