【问题标题】:How to give a custom shape to a Tab Control in WPF?如何为 WPF 中的选项卡控件提供自定义形状?
【发布时间】:2020-11-05 02:04:46
【问题描述】:

我在 WPF 中创建了一个应用程序,我需要使用 TabControl 在内容之间切换。但出于设计目的,我的 Tab 项内容位于同一水平线上,后跟 Tab 项标题。

这是我的 Mainwindow.xaml 的代码

<Grid Width="635" HorizontalAlignment="Left" Height="458" VerticalAlignment="Top" Margin="0,61,0,0" >



            <TabControl Margin="-1" BorderThickness="0" Background="#222222" >
               
                <TabItem   Style="{StaticResource TabItemDefaults}"                            Header="File manager"  FontSize="10" Foreground="#efefef" Margin="5,0,0,0" Width="97" Height="20"   FontFamily="Segoe UI" UseLayoutRounding="True" TextOptions.TextFormattingMode="Display"                        >

                    <Grid>
                        <Label Name="Folder" Content="Folder:"  FontSize="10" Foreground="#efefef"   Height="20" Width="40"  Margin="-571,-367,0,0"   FontFamily="Segoe UI" UseLayoutRounding="True" TextOptions.TextFormattingMode="Display"                                  />
                        <Button Name ="FolderSelect"                               Width="532" HorizontalAlignment="Left" VerticalAlignment="Top"  Height="33" Margin="85,17,0,0"     Background="#1a1a1a"   
                            
                            materialDesign:ShadowAssist.ShadowDepth="Depth0" materialDesign:RippleAssist.Feedback="Transparent" BorderThickness="0"  UseLayoutRounding="True"
                            
                            ></Button>
                        <Label Name="ShowFolders" Content=".." Margin="-479,59,0,0"  Background="#1a1a1a"  Width="168" Height="373"  Foreground="#efefef" ></Label>


                        <Button
    Style="{StaticResource MaterialDesignRaisedButton}"
                           
                            
    Width="65" HorizontalAlignment="Left" Height="25" Background="#FF403D3D" Margin="16,292,0,0" 
                            
                           
    ToolTip="Resource name: MaterialDesignRaisedButton">
                            <materialDesign:PackIcon Kind="PlusThick" />

                        </Button>

                        <Button
    Style="{StaticResource MaterialDesignRaisedButton}"
    Width="65" HorizontalAlignment="Left" Height="25" Background="#FF403D3D" Margin="82,292,0,0" 
    ToolTip="Open output folder">
                            <materialDesign:PackIcon Kind="FolderUpload" />
                        </Button>

                        <Label Content="Video recordings:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="269.2,-22,0,0" Foreground="#efefef"  FontSize="10"  FontFamily="Segoe UI" UseLayoutRounding="True" TextOptions.TextFormattingMode="Display" />
    
                    </Grid>    
                </TabItem>
                <TabItem   Style="{StaticResource TabItemDefaults}"                                           Header ="Preview"   FontSize="10" Foreground="#efefef" Width="67" Height="20" Margin="-8,0,0,0"  FontFamily="Segoe UI" UseLayoutRounding="True" TextOptions.TextFormattingMode="Display"                  >

                    <Grid>
                        <CheckBox Content="Draggable mode" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="520,-22,0,0"     Width="110"                                    />


                    </Grid>

                </TabItem>

            </TabControl>


        </Grid>

Tab Control 和 Tab 项都是由我在 App.xaml 中声明的控件模板制作的。

这是我在 App.xaml 中单独声明的 Tab Control 的代码 -

<Style  TargetType="{x:Type TabControl}"  x:Key="TabControlDefaults"  x:Name="NewTabcontrol" >
                <Setter Property="OverridesDefaultStyle"
          Value="True" />
                <Setter Property="SnapsToDevicePixels"
          Value="True" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabControl}">
                            <Grid KeyboardNavigation.TabNavigation="Local">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                Storyboard.TargetProperty="(Border.BorderBrush).
                    (SolidColorBrush.Color)">
                                                    <EasingColorKeyFrame KeyTime="0"
                                         Value="#FFAAAAAA" />
                                                </ColorAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <TabPanel x:Name="HeaderPanel"
                    Grid.Row="0"
                    Panel.ZIndex="1"
                    Margin="0,0,4,-1"
                    IsItemsHost="True"
                    KeyboardNavigation.TabIndex="1"
                    Background="Transparent" />
                                <Border x:Name="Border"
                  Grid.Row="1"
                  BorderThickness="1"
                  CornerRadius="2"
                  KeyboardNavigation.TabNavigation="Local"
                  KeyboardNavigation.DirectionalNavigation="Contained"
                  KeyboardNavigation.TabIndex="2">
                                    <Border.Background>
                                        <LinearGradientBrush EndPoint="0.5,1"
                                   StartPoint="0.5,0">
                                            <GradientStop Color="{DynamicResource ContentAreaColorLight}"
                              Offset="0" />
                                            <GradientStop Color="{DynamicResource ContentAreaColorDark}"
                              Offset="1" />
                                        </LinearGradientBrush>
                                    </Border.Background>
                                    <Border.BorderBrush>
                                        <SolidColorBrush Color="{DynamicResource BorderMediumColor}"/>
                                    </Border.BorderBrush>
                                    <ContentPresenter x:Name="PART_SelectedContentHost"
                              Margin="4"
                              ContentSource="SelectedContent" />
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

这是我在 App.xaml 中单独声明的选项卡项的代码 -

<Style TargetType="{x:Type TabItem}" x:Key="TabItemDefaults" x:Name="NewTabitem"   >

                <Setter Property="Foreground" Value="#bababa" />

                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabItem}">
                            <Grid x:Name="Panel"    ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local"                                                      >
                                <ContentPresenter x:Name="ContentSite"
                                        VerticalAlignment="Center"
                                        HorizontalAlignment="Center"
                                        ContentSource="Header"
                                        Margin="10,2"/>
                            
                            
                            </Grid>
                            
                           
                            <ControlTemplate.Triggers>


                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Foreground" Value="Black"/>
                                </Trigger>



                                <Trigger Property="IsSelected" Value="True">
                                    <!--    <Setter TargetName="Panel" Property="Background" Value="LightSkyBlue"  /> -->
                                    <Setter Property="HeaderTemplate">
                                        <Setter.Value>
                                            <DataTemplate>
                                                <TextBlock Text="{TemplateBinding Content}">

                                                    <TextBlock.TextDecorations>
                                                        <TextDecoration  PenOffset="4" PenOffsetUnit="Pixel" >
                                                            <TextDecoration.Pen>
                                                                <Pen Brush="#673ab7" Thickness="4" />
                                                            </TextDecoration.Pen>
                                                        </TextDecoration>
                                                    </TextBlock.TextDecorations>

                                                </TextBlock>
                                            </DataTemplate>
                                        </Setter.Value>
                                    </Setter>


                                </Trigger>
                                <Trigger Property="IsSelected" Value="False">
                                    <Setter TargetName="Panel" Property="Background" Value="#212121" />
                                    <Setter Property="Foreground" Value="WhiteSmoke"/>

                                </Trigger>

                              
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

我的问题是复选框(我给了一个红色圆圈)没有正常工作。我知道主要原因是复选框位于选项卡控件的选项卡面板之外。但是为了我的应用程序的设计目的,我必须把它放在那里。

那么,我如何为 Tab 控件的选项卡面板提供自定义形状,以便我将内容元素完美地停靠在我想要的位置。

【问题讨论】:

    标签: wpf xaml checkbox tabcontrol tabitem


    【解决方案1】:

    您的 CheckBox 不能被选中,您可以使用 DataTrigger 使 chexkbox 可见或隐藏,具体取决于是否选择了 Preview TabItem。

    我对您的 MainWindow.xaml 进行了一些更新,以使复选框可以被选中。

      <StackPanel Width="635" HorizontalAlignment="Left" Height="458" VerticalAlignment="Top" Margin="0,61,0,0" >
    
        <CheckBox Content="Draggable mode" HorizontalAlignment="Right" VerticalAlignment="Top" Width="110" >
            <CheckBox.Style>
                <Style TargetType="CheckBox">
                    <Setter Property="Visibility" Value="Hidden"></Setter>
                    <Setter Property="IsEnabled" Value="False"></Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=myTabControl,Path=SelectedItem.Header}" Value="Preview" >
                            <Setter Property="Visibility" Value="Visible"></Setter>
                            <Setter Property="IsEnabled" Value="True"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </CheckBox.Style>
        </CheckBox>
    
        <TabControl BorderThickness="0" Background="#222222"  Name="myTabControl" SelectionChanged="myTabControl_SelectionChanged">
    
            <TabItem   Style="{StaticResource TabItemDefaults}" Header="File manager"  FontSize="10" Foreground="#efefef" Margin="5,0,0,0" Width="97" Height="20"   FontFamily="Segoe UI" UseLayoutRounding="True" TextOptions.TextFormattingMode="Display"                        >
                <Grid Height="300">
                </Grid>
            </TabItem>
            <TabItem Style="{StaticResource TabItemDefaults}" Header ="Preview"   FontSize="10" Foreground="#efefef" Width="67" Height="20"  FontFamily="Segoe UI" UseLayoutRounding="True" TextOptions.TextFormattingMode="Display"                  >
                <Grid Height="300">
                </Grid>
            </TabItem>
        </TabControl>
    </StackPanel>
    

    【讨论】:

    • 您的回答是正确而精彩的,但由于设计目的,我需要对齐 TabControl。这意味着 TabControl 边距应与 Stackpanel 边距匹配。我运行您的代码,发现 TabControl 位于底部位置。如果将 Margin 与我的设计完全相同,则会发生同样的问题,复选框未选中。虽然我使用了 DataTrigger 但仍然没有被选中。但如果我稍微失望,那么它会再次起作用。你能帮帮我吗?
    • 我解决了这个问题,非常感谢你的帮助,你是上帝的天赋@DasiyTian MSFT,我必须在不同的 Stackpanel 中选择复选框。这意味着我必须使用两个 Stackpanel,一个用于 TabControl,另一个用于 Checkbox,之后我使用 Data 触发器,这样 Checkbox 就不会被 Tabcontrol 覆盖。现在我根据需要给出确切的保证金。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多