3

我有一个TabControl,它的 ItemsSource 绑定到一个可观察的视图集合(UserControls),每个视图都以TabItem作为其根元素。但是,当它显示时,标题文本在每个 TabItem 的内容中,好像UserControl包装器导致冲突:

替代文字

TabControl 位于SmartFormView.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>

为了让 TabItems 在 TabControl 中显示为 TabItems,我需要进行哪些更改?

以下是名为SmartFormAreaView.xaml 的 TabItem 视图:

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

这里是我创建每个视图并将其加载到ObservableCollection的地方:

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

2 回答 2

4

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

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

<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。每个 View 的 DataContext 将自动设置为适当的 Presenter。

有关各种 ItemsControl 相关主题的精彩讨论,请参阅 WPF 博士的博客

于 2009-08-04T10:47:20.047 回答
0

只有当TabControl它们可以转换为TabItem、而不是 UserControl 或 SmartFormAreaView 等时,才会接受您的控件作为其控件。

TabItems所以你要么用你的可视化树填充常规,要么你子类TabItems,或者你子类TabControl来覆盖它的IsItemItsOwnContainerOverride方法,接受你的类型作为容器。

该方法应如下所示:

protected override bool IsItemItsOwnContainerOverride(object item)
{
    return item is YourControlTypeHere || item is TabItem;
}
于 2009-08-04T10:12:36.323 回答