總結一下WPF中Style樣式的引用方法。
一、內聯樣式:
直接設置控件的Height、Width、Foreground、HorizontalAlignment、VerticalAlignment等屬性。
以設置一個Botton控件的樣式為例,如:
<Button Content="Button" Name="btnDemo" Height="72" Width="150" Foreground="White" Background="Blue" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="170,132,0,0" />
這種方式比較簡單,但是代碼不能復用。
二、嵌入樣式:
在頁面<Window.Resources>節點下添加樣式,然后在需要的控件上設置Style屬性。還是以Botton控件為例。
1、在頁面<Window.Resources>節點下添加一個Key值叫“myBtnStyle”的樣式
<Window.Resources> <Style x:Key="myBtnStyle" TargetType="{x:Type Button}"> <Setter Property="Height" Value="72" /> <Setter Property="Width" Value="150" /> <Setter Property="Foreground" Value="Red" /> <Setter Property="Background" Value="Black" /> <Setter Property="HorizontalAlignment" Value="Left" /> <Setter Property="VerticalAlignment" Value="Top" /> </Style> </Window.Resources>
2、 設置Botton控件的Style屬性為"{StaticResource BtnStyle}"
<Button Content="Button" Name="btnDemo" Style="{StaticResource BtnStyle}"/>
TargetType="{x:Type Button}"指定了該樣式適用於Botton類型的控件,Key="myBtnStyle"如果不設置該值,則該樣式將適用於所有的Botton控件,而設置了其值為“myBtnStyle”,則只用於設置了 Style="{StaticResource myBtnStyle}"的Botton控件。這就好比CSS中的元素選擇器和類選擇器。
這種方式可以使得單個頁面上的控件能夠復用一個樣式,比第一種方式面向對象了一步。
三、外聯樣式:
1、新建一個.xaml資源文件,如/Theme/Style.xaml
2、 在Style.xaml文件里編寫樣式代碼
Style.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib"> <Style x:Key="myBtnStyle" TargetType="Button"> <Setter Property="Height" Value="72" /> <Setter Property="Width" Value="150" /> <Setter Property="Foreground" Value="White" /> <Setter Property="Background" Value="Blue" /> <Setter Property="HorizontalAlignment" Value="Left" /> <Setter Property="VerticalAlignment" Value="Top" /> </Style> </ResourceDictionary>
3、在App.xaml文件的<Application.Resources>
或者普通頁面的<Window.Resources>
或者用戶控件的 <UserControl.Resources> 節點下
添加相應的ResourceDictionary,配置引用Style.xaml:
app.xaml:
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/應用名稱;component/Theme/Style.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
或者MainWindow.xaml:
<Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Theme/BtnStyle.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources>
<ResourceDictionary.MergedDictionaries>節點下可以添加多個資源文件
這種方式相比前面兩種使得樣式和結構又更進一步分離了。
在App.xaml引用,是全局的,可以使得一個樣式可以在整個應用程序中能夠復用。在普通頁面中引用只能在當前頁面上得到復用。
4、設置Botton控件的Style屬性為"{StaticResource myBtnStyle}" 和上面的一樣。
四、用C#代碼動態加載資源文件並設置樣式
1、新建資源文件:同上面的1,2兩步。
2、在后台編寫代碼
首先,將我們自定義的樣式加載到應用程序的資源字典中。
ResourceDictionary resourceDictionary =newResourceDictionary(); Application.LoadComponent(resourceDictionary, new Uri("/PhoneApp1;component/Resources/BtnStyle.xaml", UriKind.Relative)); Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
其次,為控件添加樣式。
this.btnDemo.SetValue(Button.StyleProperty, Application.Current.Resources["BtnStyle"]);