0

我需要同时使用 ControlTemplate 和 DataTemplate - 我想。

我有一个 TabControl,它的 TabItems 由 ObservableCollection 提供 - 我可以根据自己的选择设计 ObservableCollection 项目。

TabItems 需要使用 ControlTemplate,因为它们具有选中、未选中和禁用状态,因此它们需要有事件触发器来切换状态 - 选中的标签有一个橙色玻璃按钮,未选中的标签有一个蓝色玻璃按钮. 每个选项卡都需要有一个位于相应玻璃按钮内的图标(图像)以及一个选项卡标签,每个选项卡都不同,由 ObservableCollection 提供。

如果我理解正确,可以使用 ControlTemplate 或 DataTemplate 但不能同时使用同一可视树级别。

我能想到的唯一可能的解决方案是在 TabItem ControlTemplate 中定义一个 ContentPresenter - ContentPresenter(带有图像和标签)将由 DataTemplate 定义,该 DataTemplate 将从 ObservableCollection 接收数据。

我的理解中的任何指示、建议和/或更正将不胜感激。

4

2 回答 2

0

有两个步骤可以做到这一点。

在选项卡控件中,您可以使用 ItemContainerStyle <- 甚至在列表框和项目控件中使用,您可以在其中指定“ContentPresenter”周围的“周围”UI,这可以为您提供状态绑定工具,如启用、选择等。

您也可以继续使用 DataTemplate。您的数据模板将在 Item Container 的 Style 的“ContentPresenter”中呈现。

于 2009-07-17T14:57:40.070 回答
0

您能否更具体一点,您想要实现什么行为?通常,您可以根据绑定的数据项单独设置选项卡标题和选项卡内容的样式。正如您可以轻松推断的那样,itemtemplate 描述了标题,而 contenttemplate 定义了它的内容。下面的示例显示了如何:(包括

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:sys="clr-namespace:System;assembly=mscorlib" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

在周围的页面/窗口中)

<Grid>
  <Grid.Resources>
     <x:Array x:Key="data" Type="{x:Type sys:Type}">
        <x:Type TypeName="Visual"/>
        <x:Type TypeName="UIElement"/>
        <x:Type TypeName="FrameworkElement"/>
     </x:Array>
  </Grid.Resources>
  <TabControl ItemsSource="{StaticResource data}">
     <TabControl.ItemTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding Name}">
              <TextBlock.Style>
                 <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Background" Value="DimGray"/>
                    <Setter Property="Foreground" Value="White"/>
                 </Style>
              </TextBlock.Style>
           </TextBlock>
        </DataTemplate>
     </TabControl.ItemTemplate>
     <TabControl.ContentTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding FullName}">
           <TextBlock.Style>
              <Style TargetType="{x:Type TextBlock}">
                 <Setter Property="Background" Value="LightCoral"/>
                 <Setter Property="Foreground" Value="Navy"/>
              </Style>
              </TextBlock.Style>
           </TextBlock>
        </DataTemplate>
     </TabControl.ContentTemplate>
  </TabControl>
</Grid>
于 2009-07-13T21:48:42.957 回答