WPF 标签样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21637380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:46:24  来源:igfitidea点击:

WPF Tab Styling

wpfxamltabcontrol

提问by Andez

I am trying to style my WPF TabControl. I basically want to get the tab control to have a transparent background, with a white border and text. I want the selected tab to have a White Background and Transparent Text (or any colour text!). Essentially a 2 colour tab.

我正在尝试设置 WPF TabControl 的样式。我基本上想让选项卡控件具有透明背景,带有白色边框和文本。我希望所选选项卡具有白色背景和透明文本(或任何颜色文本!)。本质上是一个 2 色标签。

However, I cannot override the selected tab appearance - this shows as white. And my child textboxes are taking the style of the TabItem font. Note, in the screen shot my labels have their own style set so do not take the TabItem font.

但是,我无法覆盖选定的选项卡外观 - 这显示为白色。我的子文本框采用 TabItem 字体的样式。请注意,在屏幕截图中,我的标签设置了自己的样式,因此不要使用 TabItem 字体。

ScreenShot

截屏

I have the following XAML in place to do this. Ideally I want to create the styles so that I can reuse across the application.

我有以下 XAML 来执行此操作。理想情况下,我想创建样式以便我可以在整个应用程序中重用。

Resource Dictionary

资源字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
  <Style x:Key="Tabs" TargetType="TabControl">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="White"/>
  </Style>
  <Style x:Key="TabItemStyle" TargetType="TabItem">
        <Setter Property="Foreground" Value="White" />
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="FontSize" Value="16" />
        <Setter Property="Background" Value="Transparent"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=TabItem}}" Value="True">
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="Background" Value="White"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=TabItem}}" Value="False">
                <Setter Property="Foreground" Value="LightGray"/>
                <Setter Property="Background" Value="Transparent"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>

Then the XAML MarkUp

然后是 XAML 标记

<TabControl Style="{StaticResource Tabs}">
  <TabItem Header="General" Style="{StaticResource TabItemStyle}">...</TabItem>
  <TabItem Header="Details" Style="{StaticResource TabItemStyle}">...</TabItem>
  <TabItem Header="Info" Style="{StaticResource TabItemStyle}">...</TabItem>
  <TabItem Header="More Stuff..." Style="{StaticResource TabItemStyle}">...</TabItem>
</TabControl>

How can I style my tabs to be and prevent the children from sizing?

我怎样才能设计我的标签并防止孩子们调整大小?

回答by dkozl

Your DataTriggersdon't work. To fix it change RelatveSourceto Self

DataTriggers不工作。要修复它,请更改RelatveSourceSelf

Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}"

However I would suggest to change them to Triggerslike so:

但是我建议将它们更改为Triggers这样:

<Style.Triggers>
   <Trigger Property="IsSelected" Value="True">
      <Setter Property="Foreground" Value="Red"/>
      <Setter Property="Background" Value="White"/>
   </Trigger>
   <Trigger Property="IsSelected" Value="False">
      <Setter Property="Foreground" Value="LightGray"/>
      <Setter Property="Background" Value="Transparent"/>
   </Trigger>
</Style.Triggers>

回答by Jan

You should create control template for TabItem.

您应该为 TabItem 创建控件模板。

This sample change TabItem background to Transparent and Text color to White.

此示例将 TabItem 背景更改为透明,将文本颜色更改为白色。

You can use own color schema.

您可以使用自己的颜色模式。

<Window.Resources>
    <Style TargetType="TabControl">
        <Setter Property="Background"
                Value="Transparent" />
        <Setter Property="BorderBrush"
                Value="White" />
    </Style>

    <Style TargetType="{x:Type TabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid>
                        <Border Name="Border"
                                Margin="0,0,-4,0"
                                Background="{x:Static Brushes.White}"
                                BorderBrush="{x:Static Brushes.White}"
                                BorderThickness="1,1,1,1"
                                CornerRadius="2,12,0,0">
                            <ContentPresenter x:Name="ContentSite"
                                              Margin="12,2,12,2"
                                              HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              ContentSource="Header"
                                              RecognizesAccessKey="True" />
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected"
                                 Value="True">
                            <Setter Property="Panel.ZIndex"
                                    Value="100" />
                            <Setter TargetName="Border"
                                    Property="Background"
                                    Value="{x:Static Brushes.Transparent}" />
                            <Setter TargetName="Border"
                                    Property="BorderThickness"
                                    Value="1,1,1,0" />
                            <Setter Property="TextBlock.Foreground"
                                    Value="White" />
                            <!--<Setter Property="TextBlock.Foreground"
                                    Value="Transparent" />-->
                        </Trigger>
                        <Trigger Property="IsEnabled"
                                 Value="False">
                            <Setter TargetName="Border"
                                    Property="Background"
                                    Value="{x:Static Brushes.White}" />
                            <Setter TargetName="Border"
                                    Property="BorderBrush"
                                    Value="{x:Static Brushes.White}" />
                            <Setter Property="Foreground"
                                    Value="{x:Static Brushes.White}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid Background="SkyBlue">
    <TabControl Margin="20">
        <TabItem Header="TabItem #1">
            <TextBox>Tab Item #1 content</TextBox>
        </TabItem>
        <TabItem Header="TabItem #2">
            <TextBox>Tab Item #1 content</TextBox>
        </TabItem>
        <TabItem Header="TabItem #3">
            <TextBox>Tab Item #1 content</TextBox>
        </TabItem>
    </TabControl>
</Grid>