1、前台myWindow.xaml文件中的代码
<TabControl x:Name="menuTab" Grid.RowSpan="2" Margin="0" Style="{DynamicResource TabControlStyle}" Grid.Row="1" Background="{x:Null}">
<TabItem Header="系统设置" Height="83" Margin="80,0,0,0" Width="74" Style="{DynamicResource TabItemStyle}">
<TabItem.Background>
<ImageBrush ImageSource="skin/ico/ico_dsmain.png"/> <!--这里图片需要替换才能正常运行-->
</TabItem.Background>
<Grid Background="{DynamicResource MyBrush}"/>
</TabItem>
<!--这里在后台动态添加和上面系统设置类似的TabItem和对应的样式等属性-->
</TabControl>
2、后台myWindow.xaml.cs文件中的代码
private void Button_Click(object sender, RoutedEventArgs e)
{
//动态添加子菜单
TabItem myDnymicTab = new TabItem() { Header = "用户管理", Height = 83, Width = 74 };
//设置图片
ImageBrush myImageBrush=new ImageBrush(new BitmapImage(new Uri(@"../../skin/ico/ico_PluginCleaner.png", UriKind.Relative)));
myDnymicTab.Background=myImageBrush;
//设置位置
Thickness myThickness =new Thickness(120,0,0,0);
myDnymicTab.Margin=myThickness;
//设置样式
Style myStyle = (Style)this.FindResource("TabItemStyle");//TabItemStyle这个样式是引用的资源文件中的样式名称
myDnymicTab.Style = myStyle;
//添加TabItem到TabControl中
menuTab.Items.Add(myDnymicTab);
menuTab.SelectedItem = myDnymicTab;
}
3、App.xaml中添加样式字典文件引用
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--App.xaml资源样式-->
<ResourceDictionary Source="TabControlStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
4、资源文件TabControlStyle.xaml中的样式:
<!-- 应该在此定义资源字典条目。-->
<Style x:Key="TabControlStyle" TargetType="{x:Type TabControl}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Padding" Value="4,4,4,4"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="{StaticResource TabControlNormalBorderBrush}"/>
<Setter Property="Background" Value="#F9F9F9"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabControl}">
<Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="ColumnDefinition0" Width="0.192*" />
<ColumnDefinition x:Name="ColumnDefinition1" Width="0.808*"/>
</Grid.ColumnDefinitions>
<Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="0" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local" Grid.ColumnSpan="1" Grid.RowSpan="1" Width="Auto" Margin="0">
<ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Width="Auto" Margin="0"/>
</Border>
<StackPanel x:Name="HeaderPanel" Margin="0" IsItemsHost="True">
<StackPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#7AFFFFFF" Offset="0"/>
<GradientStop Color="#42F0FCFF" Offset="1"/>
</LinearGradientBrush>
</StackPanel.Background>
</StackPanel>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="TabItemStyle" TargetType="{x:Type TabItem}">
<Setter Property="FocusVisualStyle" Value="{StaticResource TabItemFocusVisual}"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Padding" Value="6,1,6,1"/>
<Setter Property="BorderBrush" Value="{StaticResource TabControlNormalBorderBrush}"/>
<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid SnapsToDevicePixels="true">
<Grid.RowDefinitions>
<RowDefinition Height="0.69*"/>
<RowDefinition Height="0.31*"/>
</Grid.RowDefinitions>
<Border x:Name="Bd" BorderThickness="0" CornerRadius="3" BorderBrush="Black" Margin="0" Grid.RowSpan="2" Visibility="Hidden">
<Border.Background>
<ImageBrush ImageSource="skin/ico/toolbar_pushed.png"/>
</Border.Background>
</Border>
<Border x:Name="fg" BorderThickness="0" CornerRadius="3" BorderBrush="Black" Margin="0" Grid.RowSpan="2" Visibility="Hidden" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
<Border.Background>
<ImageBrush ImageSource="skin/ico/toolbar_hover.png"/>
</Border.Background>
</Border>
<TextBlock Margin="0,0.333,0,3.833" TextWrapping="Wrap" VerticalAlignment="Stretch" d:LayoutOverrides="Height" Grid.Row="1" HorizontalAlignment="Center" Text="{TemplateBinding Header}" Foreground="White"/>
<Border x:Name="ico" BorderThickness="0" CornerRadius="3" BorderBrush="Black" Margin="4,4,4.25,0" Grid.RowSpan="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="48" Height="48" Background="{TemplateBinding Background}" RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true"/>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Visibility" TargetName="Bd" Value="Visible"/>
<Setter Property="Panel.ZIndex" TargetName="ico" Value="1"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="false"/>
<Condition Property="IsMouseOver" Value="true"/>
</MultiTrigger.Conditions>
<Setter Property="Visibility" TargetName="fg" Value="Visible"/>
<Setter Property="RenderTransform" TargetName="ico">
<Setter.Value>
<TransformGroup>
<ScaleTransform ScaleX="1.05" ScaleY="1.05"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Setter.Value>
</Setter>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
分享到:
相关推荐
"WPF后台动态调用样式文件"一文对应的工程项目代码
代码有WPF分页的实现; TabControl的TabItem左侧排列且文字竖排的实现!
最近尝试用wpf在后台动态添加控件,还有个滚动条效果,基本已完成,希望对大家有帮助
WPF 实现TabControl 中动态添加选项卡、动态添加Frame
WPF DataGrid 动态添加 行列 比较靠谱的,可以看看,通俗易懂
1、采用WPF; 2、支持菜单栏,工具栏; 3、标题栏可修改背景色; 4、TabControl动态添加、删除(支持左右滑动,选择)
WPF:TabItem的Header拉伸的TabControl
C# WPF 在ListView 中 动态添加ProgressBar, 开发环境vs
WPF的TabControl左侧的TabItem的Header纵线显示
WPF的ListView简易操作示例,如何在代码中按需设置数据源、追加行或列。一个简单的示例说明,便于了解基本应用,供有需要的进行学习。
有一个很好的例子,如何使 书签,圆边 通过修改样式WPF TabControl和TabItem的,但它不支持底部标签TabStripPlacement的位置»底部。 样品的一个小的修改,允许从底部放置一个书签。 为VisualStudio 2010下载完整的...
WPF控件动态添加子控件
wpf radiobutton模版设计成button样式
WPF后台线程更新UI---http://www.tuicool.com/articles/NbieE3M
WPF工程,通过代码动态生成Label,并且label的位置根据界面的大小来确定,个数根据用户输入的个数来确定。 同时双击label之后会跳出ColorDialog界面,通过的ColorDialog中选中的颜色来修改label中的颜色
WPF 后台管理UI 界面
本文介绍如何将关闭按钮添加到WPF TabItem。 此关闭按钮将仅显示在选定的选项卡上。
根据传值控件名称(如TextBox,CheckBox,ComboBox,CheckBoxList,DatePicker等) 动态生成控件,选中或设置值后取值
WPFButton样式,6种button样式,简单的按钮样式,美化界面按钮
WPF.Themes.ThemeManager.ApplyTheme(Application.Current, Properties.Settings.Default.Themes);//读取设置界面主题配置信息 即可使所有常用的控件主题变成所选择的主题,无需在每个窗体里修改样式。 减少了很...