WPF后台动态添加TabItem并设置样式

来源:互联网 发布:怎么找文献的数据 编辑:程序博客网 时间:2024/05/01 05:09

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>