首页 > 其他分享 >WPF TabControl 简单样式自定义

WPF TabControl 简单样式自定义

时间:2022-12-13 13:12:01浏览次数:57  
标签:控件 自定义 样式 简单 WPF TabControl

WPF TabControl 简单样式自定义,覆写控件模版,在此记录下

  1  <!-- SimpleStyles: TabControl -->
  2                     <Style TargetType="{x:Type TabControl}">
  3                         <Setter Property="OverridesDefaultStyle" Value="True"/>
  4                         <Setter Property="Padding" Value="2"/>
  5                         <Setter Property="HorizontalContentAlignment" Value="Left"/>
  6                         <Setter Property="VerticalContentAlignment" Value="Center"/>
  7                         <Setter Property="Background" Value="Transparent"/>
  8                         <Setter Property="BorderBrush" Value="#FFACACAC"/>
  9                         <Setter Property="BorderThickness" Value="1"/>
 10                         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
 11                         <Setter Property="Template">
 12                             <Setter.Value>
 13                                 <ControlTemplate TargetType="{x:Type TabControl}">
 14                                     <Grid x:Name="templateRoot" ClipToBounds="True" SnapsToDevicePixels="True" KeyboardNavigation.TabNavigation="Local">
 15                                         <Grid.ColumnDefinitions>
 16                                             <ColumnDefinition x:Name="ColumnDefinition0"/>
 17                                             <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
 18                                         </Grid.ColumnDefinitions>
 19                                         <Grid.RowDefinitions>
 20                                             <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
 21                                             <RowDefinition x:Name="RowDefinition1" Height="*"/>
 22                                         </Grid.RowDefinitions>
 23                                         <TabPanel Name="HeaderPanel" Grid.Column="0" Grid.Row="0" Margin="0"
 24                                                   HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
 25                                                   Background="Transparent"  
 26                                                   IsItemsHost="True" 
 27                                                   KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
 28                                         <Border Name="ContentPanel" Grid.Column="0" Grid.Row="1"
 29                                                 BorderBrush="{TemplateBinding BorderBrush}"
 30                                                 BorderThickness="{TemplateBinding BorderThickness}"
 31                                                 Background="{TemplateBinding Background}"
 32                                                 KeyboardNavigation.DirectionalNavigation="Contained"  
 33                                                 KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
 34                                             <ContentPresenter Name="PART_SelectedContentHost"  Margin="0"
 35                                                               ContentTemplate="{TemplateBinding SelectedContentTemplate}"
 36                                                               Content="{TemplateBinding SelectedContent}"
 37                                                               ContentStringFormat="{TemplateBinding SelectedContentStringFormat}"
 38                                                               ContentSource="SelectedContent"
 39                                                               SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
 40                                         </Border>
 41                                     </Grid>
 42                                     <ControlTemplate.Triggers>
 43                                         <Trigger Property="TabStripPlacement" Value="Bottom">
 44                                             <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/>
 45                                             <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
 46                                             <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
 47                                             <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
 48                                         </Trigger>
 49                                         <Trigger Property="TabStripPlacement" Value="Left">
 50                                             <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
 51                                             <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
 52                                             <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>
 53                                             <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>
 54                                             <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
 55                                             <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
 56                                             <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
 57                                             <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
 58                                         </Trigger>
 59                                         <Trigger Property="TabStripPlacement" Value="Right">
 60                                             <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
 61                                             <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
 62                                             <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/>
 63                                             <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/>
 64                                             <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
 65                                             <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
 66                                             <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
 67                                             <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
 68                                         </Trigger>
 69                                         <Trigger Property="IsEnabled" Value="False">
 70                                             <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
 71                                         </Trigger>
 72                                     </ControlTemplate.Triggers>
 73                                 </ControlTemplate>
 74                             </Setter.Value>
 75                         </Setter>
 76                     </Style>
 77 
 78                     <!-- SimpleStyles: TabItem -->
 79                     <Style TargetType="{x:Type TabItem}">
 80                         <Setter Property="OverridesDefaultStyle" Value="True"/>
 81                         <Setter Property="Foreground" Value="Black"/>
 82                         <Setter Property="Background" Value="#4CE4E4E4"/>
 83                         <Setter Property="BorderBrush" Value="#FFACACAC"/>
 84                         <Setter Property="BorderThickness" Value="1"/>
 85                         <Setter Property="Margin" Value="0"/>
 86                         <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
 87                         <Setter Property="VerticalContentAlignment" Value="Stretch"/>
 88                         <Setter Property="Template">
 89                             <Setter.Value>
 90                                 <ControlTemplate TargetType="{x:Type TabItem}">
 91                                     <Grid x:Name="templateRoot" SnapsToDevicePixels="True" Background="Transparent">
 92                                         <Border Name="Border" Margin="{TemplateBinding Margin}"
 93                                                 BorderThickness="{TemplateBinding BorderThickness}" 
 94                                                 CornerRadius="2,12,0,0"
 95                                                 Background="{TemplateBinding Background}"
 96                                                 BorderBrush="{TemplateBinding BorderBrush}" >
 97                                             <ContentPresenter x:Name="ContentSite" 
 98                                                               VerticalAlignment="Center"
 99                                                               HorizontalAlignment="Center"
100                                                               ContentSource="Header"
101                                                               Margin="10,3,10,3" RecognizesAccessKey="True"/>
102                                         </Border>
103                                     </Grid>
104                                     <ControlTemplate.Triggers>
105                                         <MultiDataTrigger>
106                                             <MultiDataTrigger.Conditions>
107                                                 <Condition Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="false"/>
108                                             </MultiDataTrigger.Conditions>
109                                             <Setter Property="Opacity" TargetName="templateRoot" Value="0.56"/>
110                                         </MultiDataTrigger>
111 
112                                         <MultiDataTrigger>
113                                             <MultiDataTrigger.Conditions>
114                                                 <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true"/>
115                                             </MultiDataTrigger.Conditions>
116                                             <Setter Property="Panel.ZIndex" Value="1"/>
117                                             <Setter Property="Background" TargetName="Border" Value="#FFA8D6F5"/>
118                                         </MultiDataTrigger>
119                                         <MultiDataTrigger>
120                                             <MultiDataTrigger.Conditions>
121                                                 <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="false"/>
122                                             </MultiDataTrigger.Conditions>
123                                             <Setter Property="Panel.ZIndex" Value="1"/>
124                                             <Setter Property="Background" TargetName="Border" Value="{Binding Background}"/>
125                                         </MultiDataTrigger>
126                                     </ControlTemplate.Triggers>
127                                 </ControlTemplate>
128                             </Setter.Value>
129                         </Setter>
130                     </Style>

测试使用

1   <TabControl>
2                 <TabItem Header="TabItem1" Width="100">
3 
4                 </TabItem>
5 
6                 <TabItem Header="TabItem2" Width="100">
7                     
8                 </TabItem>
9    </TabControl>

效果图

 

标签:控件,自定义,样式,简单,WPF,TabControl
From: https://www.cnblogs.com/VueDi/p/16978030.html

相关文章

  • 关于el-descriptions-item自定义使用方法
    如果有错请及时点出,虚心接受各位大佬提醒-----------开发不容易且行且珍惜显示类似于原生的table表格的列形式exportdefault{components:{},name:'n......
  • Spring 自定义propertyEditor
    Userpackagecom.example.zylspringboot.selfEditor;publicclassUser{privateStringname;privateAddressaddress;privateIntegerage;......
  • echarts 图片自定义下载、导出
    来自:https://www.jianshu.com/p/90bf22936439侵删除   //图表导出-自己的方法toolbox:{show:true,feature:{sa......
  • 使用DevExpress WPF主题设计器轻松创建Office 2019绿色主题(二)
    DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专......
  • AntD中Form表单自定义验证规则
    <Form.Item label="学生编码" name="stuId" validateFirst={true}//出错时两个校验分别生效 rules={[ {required:true,message:'学生编码不能为空'}, {val......
  • Unity 自定义创建脚本模板
    Unity自定义创建脚本模板原理:以模板代码为底板,通过关键字替换来实现代码创建两种实现方案方案11.先准备好对应的代码模板,放到Assets\ScriptTemplates目录下usingSys......
  • WPF E 文章汇总
    MSDN"WPF/E"(codename)DevCenter:​​http://msdn2.microsoft.com/en-us/asp.net/bb187358.aspx​​​对网上的WPF/E文章汇总,前一部分来自​​思归​​的​​WPF/E......
  • 自定义分页器
    自定义分页器目录自定义分页器1、分页推导2、分页器代码封装1、分页推导queryset对象支持切片操作确定用户访问的页码url?page=1current_page=request.GET.......
  • DataGear 制作自定义柱状图条目颜色的数据可视化看板
    DataGear看板提供了dg-chart-options图表选项配置功能,可自定义样式、位置、显示内容等图表选项,其中的processUpdateOptions回调函数配置项,可以在图表更新数据前以编程方式......
  • 前端开发系列051-基础篇之自定义事件和插槽(Vue)
    title:前端开发系列051-基础篇之自定义事件和插槽(Vue)tags:categories:[]date:2017-12-1400:00:00本文是早期学习Vue整理的一些阶段性总结,内容主要关于Vue框架......