WPF後臺設置xaml控件的樣式System.Windows.Style

WPF後臺設置xaml控件的樣式System.Windows.Style

摘-自 :感謝 做者: IT小兵   http://3w.suchso.com/projecteac-tual/wpf-zhishi-houtai-shezhi-style.htmlhtml

Style myStyle = (Style)this.FindResource("TabItemStyle");//TabItemStyle 這個樣式是引用的資源文件中的樣式名稱緩存

靜態資源在第一次編譯後即肯定其對象或值,以後不能對其進行修改。動態資源則是在運行時決定,當運行過程當中真正須要時,纔到資源目標中查找其值。app

好比:ide

  <Canvas Background="{DynamicResource innerLgbResource}">this

StaticResources的適用場合:
(1)在資源第一次引用以後無需再修改資源的值。
(2)資源引用不會基於運行時的行爲進行從新計算,好比在從新加載Page/Window的時候。
(3)當須要設置的屬性不是DependencyObject或Freezable類型的時候,用StaticResource。
(4)當須要將資源編譯到dll中,並打包爲程序的一部份,或者但願在各應用程序之間共享時,也使用StaticResource。
(5)當須要爲一個自定義控件建立一個Theme,並Theme中使用資源,就須要使用StaticResource。由於StaticResource的資源查找行爲時可預測的,而且自己包含在Theme中。而對於DynamicResource,即便資源是定義在Theme中,也只能等到運行時肯定,致使一些可能意料不到的狀況發生。
(6)當須要使用資源設置大量的依賴屬性(Dependency Property)的時候。
因爲依賴屬性具備屬性系統提供的值緩存機制,因此,若是能在程序裝載時設置依賴屬性的值,這樣,依賴屬性就不須要檢查本身的值並返回最後的有效值了。
 
Dynamic Resource通常使用在以下場合:
(1)資源的值依賴一些條件,而該條件直到運行時才能肯定。
包括系統資源,或是用戶可設置的資源。好比:能夠建立引用系統屬性諸如SystemColors,SystemFonts來設置值,而這些屬性是動態的,它們的值又來自於運行環境和操做系統。
(2)爲自定義控件引用或建立Theme Style。
(3)但願在程序運行期間調整資源字典的內容時。
(4)但願資源能夠向前引用時(如上面在Canvas中引用innerLgbResource同樣)
(5)資源文件很大,但願在運行時才加載。
(6)要建立的Style的值可能來自於其它值,而這些值又依賴於Theme或用戶的設置。
(7)當引用資源的元素的父元素有可能在運行期改變,這個時候也須要使用動態資源。由於父元素的改變將致使資源查詢的範圍。spa

 

一、前臺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>
             </TabControl>

二、後臺myWindow.xaml.cs文件中的代碼code

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; }

三、App.xaml中添加樣式字典文件引用orm

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!--App.xaml資源樣式-->
            <ResourceDictionary Source="TabControlStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

四、資源文件TabControlStyle.xaml中的樣式:htm

<!-- 應該在此定義資源字典條目。-->
 <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>
相關文章
相關標籤/搜索