WPF后台設置xaml控件的樣式System.Windows.Style
摘-自 :感謝 作者: IT小兵 http://3w.suchso.com/projecteac-tual/wpf-zhishi-houtai-shezhi-style.html
Style myStyle = (Style)this.FindResource("TabItemStyle");//TabItemStyle 這個樣式是引用的資源文件中的樣式名稱
靜態資源在第一次編譯后即確定其對象或值,之后不能對其進行修改。動態資源則是在運行時決定,當運行過程中真正需要時,才到資源目標中查找其值。
比如:
<Canvas Background="{DynamicResource innerLgbResource}">
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)當引用資源的元素的父元素有可能在運行期改變,這個時候也需要使用動態資源。因為父元素的改變將導致資源查詢的范圍。
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>
</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>
