2010-08-12 119 views
2

我正在尝试创建一个基本上看起来像中心顶部的按钮和下面显示tabitem内容的内容面板上的按钮控制样式。WPF TabControl模板+ ItemContainerStyle

我对模板有点新,但到目前为止,除了一件事外,我的工作非常好。我希望能够为文本元素设置默认的背景颜色。通常情况下,我通过使用ContentPresenter和依赖项元素来完成此任务。所以像这样的东西。

<ContentPresenter TextElement.Foreground="White"/> 

这基本上使此Presenter编写的任何TextElement控件继承此属性。

现在我试图做同样的事情,但它不工作!我相信这与我的风格错误有关。

风格:

<Style x:Key="MainMenuTab" TargetType="{x:Type TabControl}"> 



     <Setter Property="Template"> 
      <Setter.Value> 

       <ControlTemplate TargetType="{x:Type TabControl}"> 

        <Grid KeyboardNavigation.TabNavigation="Local" Width="{TemplateBinding Width}"> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="*"/> 
         </Grid.RowDefinitions> 

         <!-- Tab Headers Panel --> 
         <Grid Grid.Row="0" Background="{StaticResource Brush_ApplicationTabBackground}"> 

          <TabPanel 
           Name="HeaderPanel" 
           Grid.Row="0" 
           Panel.ZIndex="1" 
           HorizontalAlignment="Center" 
           VerticalAlignment="Center" 
           IsItemsHost="True" 
           KeyboardNavigation.TabIndex="1" 
           Background="{StaticResource Brush_ApplicationTabBackground}" 
           > 


          </TabPanel> 


         </Grid> 



         <!-- Tab Body --> 
         <Border 
          Name="Border" 
          Grid.Row="1" 
          Background="{StaticResource Brush_ApplicationBackground}" 
          BorderBrush="Transparent" 
          BorderThickness="1" 
          CornerRadius="2" 
          KeyboardNavigation.TabNavigation="Local" 
          KeyboardNavigation.DirectionalNavigation="Contained" 
          KeyboardNavigation.TabIndex="2" > 

          <ContentPresenter 
           Name="PART_SelectedContentHost" 
           Margin="4" 
           ContentSource="SelectedContent" /> 
         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 




     <!-- Each Tab should look like this --> 
     <Setter Property="ItemContainerStyle"> 
      <Setter.Value> 
       <Style> 

        <Setter Property="Control.Template"> 
         <Setter.Value> 

          <ControlTemplate TargetType="{x:Type TabItem}"> 

           <Grid Background="{StaticResource Brush_ApplicationTabBackground}"> 
            <Border Width="50" x:Name="BorderTab" Height="50" Margin="5" BorderThickness="1" ClipToBounds="True" BorderBrush="Transparent" Background="Transparent" CornerRadius="5"> 
             <Rectangle x:Name="BackgroundRec" Fill="Transparent" Stroke="Transparent" Width="50" Height="50" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
            </Border> 
            <ContentPresenter Name="TheHeaderContentPresenter" Width="50" Height="50" Margin="5" ContentSource="Header" HorizontalAlignment="Center" VerticalAlignment="Center" TextElement.Foreground="White"/> 
           </Grid> 

           <ControlTemplate.Triggers> 
            <Trigger Property="IsSelected" Value="True"> 
             <Trigger.Setters> 
              <Setter TargetName="BorderTab" Property="BorderBrush" Value="{StaticResource Brush_ApplicationTabHighlight}"/> 
              <Setter TargetName="BorderTab" Property="BorderThickness" Value="3"/> 
              <Setter TargetName="BackgroundRec" Property="Fill" Value="{StaticResource Brush_ApplicationTabHighlight}"/> 
              <Setter Property="Panel.ZIndex" Value="1"/> 
             </Trigger.Setters> 
            </Trigger> 

            <Trigger Property="IsSelected" Value="False"> 
             <Trigger.Setters> 
              <Setter TargetName="BorderTab" Property="BorderBrush" Value="{StaticResource Brush_ApplicationTabBackground}"/> 
              <Setter TargetName="BorderTab" Property="BorderThickness" Value="0"/> 
              <Setter TargetName="BackgroundRec" Property="Fill" Value="{StaticResource Brush_ApplicationTabBackground}"/> 
             </Trigger.Setters> 
            </Trigger> 
           </ControlTemplate.Triggers> 

          </ControlTemplate> 

         </Setter.Value> 
        </Setter> 

       </Style> 
      </Setter.Value> 
     </Setter> 

在我ItemContainerStyle下ContentPresenter有TextElement.Foreground = “白” 属性,但它不会打印白色文字!

我使用这种风格的tabcontrol看起来是这样的:

<TabControl Grid.Row="2" Style="{StaticResource MainMenuTab}"> 


      <TabItem> 
       <TabItem.Header> 
        <TextBlock VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,5" Text="{x:Static UIStrings:ClientStrings.MainWindow_TabHeader_SingleWaveLength}"></TextBlock> 
       </TabItem.Header> 
       <TextBlock>TEST PANEL</TextBlock> 


     </TabItem> 
</TabControl> 

我知道这是compicated但我真的很喜欢这个工作。

回答

3

发现的解决方案。

基于HCL的文章,我找到了一个解决方案。我尝试了同样的确切问题,我试图让内容演示者设置继承的依赖属性。相反,我简单地告诉模板应用依赖属性,这样每个tabitem的样式都具有这个属性,因此将它设置为它的所有子节点。

<Setter Property="ItemContainerStyle"> 
    <Setter.Value> 
     <Style TargetType="TabItem"> 

      <Setter Property="TextElement.Foreground" Value="White"/> 

      <Setter Property="Template"> 
       <Setter.Value> 

        <ControlTemplate TargetType="{x:Type TabItem}"> 

         <Grid Background="{StaticResource Brush_ApplicationTabBackground}"> 
          <Border Width="50" x:Name="BorderTab" Height="50" Margin="5" BorderThickness="1" ClipToBounds="True" BorderBrush="Transparent" Background="Transparent" CornerRadius="5"> 
           <Rectangle x:Name="BackgroundRec" Fill="Transparent" Stroke="Transparent" Width="50" Height="50" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
          </Border> 
          <ContentPresenter Name="TheHeaderContentPresenter" Width="50" Height="50" Margin="5" ContentSource="Header" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
         </Grid> 

         <ControlTemplate.Triggers> 
          <Trigger Property="IsSelected" Value="True"> 
           <Trigger.Setters> 
            <Setter TargetName="BorderTab" Property="BorderBrush" Value="{StaticResource Brush_ApplicationTabHighlight}"/> 
            <Setter TargetName="BorderTab" Property="BorderThickness" Value="3"/> 
            <Setter TargetName="BackgroundRec" Property="Fill" Value="{StaticResource Brush_ApplicationTabHighlight}"/> 
            <Setter Property="Panel.ZIndex" Value="1"/> 
           </Trigger.Setters> 
          </Trigger> 

          <Trigger Property="IsSelected" Value="False"> 
           <Trigger.Setters> 
            <Setter TargetName="BorderTab" Property="BorderBrush" Value="{StaticResource Brush_ApplicationTabBackground}"/> 
            <Setter TargetName="BorderTab" Property="BorderThickness" Value="0"/> 
            <Setter TargetName="BackgroundRec" Property="Fill" Value="{StaticResource Brush_ApplicationTabBackground}"/> 
           </Trigger.Setters> 
          </Trigger> 
         </ControlTemplate.Triggers> 

        </ControlTemplate> 

       </Setter.Value> 
      </Setter> 

     </Style> 
    </Setter.Value> 
</Setter> 

所有我真的不添加一行:

<Setter Property="TextElement.Foreground" Value="White"/> 

控制模板之前!我也从内容演示者那里拿出白色文本,因为它没用。

+0

只是一个快速注意。 这最终(在我的情况下)设置所有TabItem的字体样式,而不仅仅是标题。我正在解决这个问题。 – 2010-08-12 21:15:40