1、定義 資源字典
-
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
-
<RadialGradientBrush x:Key="mybrush">
-
<GradientStop Color="#FF0000" Offset="0"/>
-
<GradientStop Color="#00ff00" Offset="1"/>
-
<GradientStop Color="#0000ff" Offset="0.6669"/>
-
</RadialGradientBrush>
-
</ResourceDictionary>
2、引用 資源字典
-
<!--定義應用程序對象-->
-
<Application x:Class="WpfApplication1.App"
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-
StartupUri="Window5.xaml" Exit="AppExit">
-
<!--StartupUri 啟動時,顯示那個xaml-->
-
<x:Code>
-
<![CDATA[
-
private void AppExit(object sender,ExitEventArgs e)
-
{
-
MessageBox.Show("App has Exit!");
-
}
-
]]>
-
</x:Code>
-
-
<Application.Resources>
-
<ResourceDictionary>
-
<ResourceDictionary.MergedDictionaries>
-
<!--外部庫名稱;Component/編譯的二進制資源名稱-->
-
<ResourceDictionary Source="/MyBrushesLibrary;Component/MyBrushes.xaml"/>
-
<ResourceDictionary Source="......"/>
-
</ResourceDictionary.MergedDictionaries>
-
</ResourceDictionary>
-
</Application.Resources>
-
</Application>
3、應用
-
<Window x:Class="WpfApplication1.Window5"
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-
Title="Window5" Height="300" Width="300">
-
<StackPanel Orientation="Vertical">
-
<Button Width="204" Height="73" Content="OK" x:Name="BtnOK" Background="{DynamicResource myBrush}" Click="BtnOK_Click"></Button>
-
<Button Width="204" Height="73" Content="Cancel" x:Name="BtnName2" Background="{StaticResource myBrush}"/>
-
</StackPanel>
-
</Window>
推薦閱讀:http://blog.csdn.net/pan_junbiao/article/details/50987932
一、樣式
1、定義樣式
-
<Window.Resources>
-
<!--定義按鈕公共樣式-->
-
<!--x:Key 引用這個樣式,TargetType作用目標類型-->
-
<Style x:Key="gButton" TargetType="{x:Type Button}">
-
<!--Setter設置目標屬性-->
-
<Setter Property="Cursor" Value="Hand"/>
-
<Setter Property="Width" Value="150"/>
-
<Setter Property="Height" Value="80"/>
-
</Style>
-
</Window.Resources>
2、引用樣式
-
<Grid>
-
<Button Content="紅色">
-
<Button.Style>
-
<!--BasedOn屬性:通過設置BasedOn屬性,可以繼承某個樣式-->
-
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource gButton}">
-
<Setter Property="Background" Value="Red"/>
-
<Setter Property="FontSize" Value="24"/>
-
<Setter Property="Foreground" Value="Red"/>
-
<Setter Property="Margin" Value="20"/>
-
</Style>
-
</Button.Style>
-
</Button>
-
</Grid>
二、樣式作用域
1、全局樣式
把樣式寫在App.xaml中即可
-
<Application x:Class="WpfApplication1.App"
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-
StartupUri="MainWindow.xaml">
-
<!--全局應用程序資源-->
-
<Application.Resources>
-
<!--定義全局樣式-->
-
-
</Application.Resources>
-
</Application>
2、局部樣式
-
<!--窗口資源-->
-
<Window.Resources>
-
<!--定義局部樣式-->
-
-
</Window.Resources>
3、內部樣式
-
<Button Content="紅色" Width="150" Height="80">
-
<Button.Style>
-
<!--定義內部-->
-
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource gBut}">
-
<!--背景色-->
-
<Setter Property="Background" Value="Red"/>
-
<!--字號-->
-
<Setter Property="FontSize" Value="24"/>
-
</Style>
-
</Button.Style>
-
</Button>
以上的樣式引用時的方法是一樣的,都是使用Resources,內部樣式的使用僅限於改控件本身,因為它沒有被放到資源字典之中,xxx.Resources是支持向下繼承的,並可以應用它在資源中的定義樣式,例如:
-
<Grid>
-
<!--定義Grid控件的資源-->
-
<Grid.Resources>
-
<!--定義按鈕樣式1-->
-
<Style x:Key="grid_button" TargetType="{x:Type Button}">
-
<Setter Property="Width" Value="260"/>
-
<Setter Property="Height" Value="160"/>
-
<Setter Property="FontSize" Value="26"/>
-
<Setter Property="Content" Value="應用父級樣式"/>
-
</Style>
-
</Grid.Resources>
-
<!--應用父級樣式的按鈕-->
-
<Button Style="{StaticResource grid_button}"></Button>
-
</Grid>
運行結果所示,XAML代碼中的按鈕除了Style="{StaticResource grid_button}"之外並沒有聲明任何屬性,按鈕的尺寸和內容都是通過應用父級Grid資源樣式來呈現的,所以只要父級的對象定義了資源,父級以下的元素均可以訪問它的資源字典。
三、Style中的Trigger
Trigger,觸發器,即當某些條件滿足時會觸發一個行為(比如某些值的變化或動畫的發生等)。觸發器比較像事件。事件一般是由用戶操作觸發的,而觸發器除了有事件觸發型的EventTrigger外還有數據變化觸發型的Trigger/DataTrigger及多條件觸發型MultiTrigger/MultiDataTrigger等。
1、基礎Trigger
Trigger類是最基本的觸發器。類似於Setter,Trigger也有Property和Value這兩個屬性,Property是Trigger關注的屬性名稱,Value是觸發條件。Trigger類還有一個Setters屬性,此屬性值是一組Setter,一旦觸發條件被滿足,這組Setter的“屬性—值”就會被應用,觸發條件不再滿足后,各屬性值會被還原。
下面這個例子中包含一個針對CheckBox的Style,當CheckBox的IsChecked屬性為true的時候前景色和字體會改變。XAML代碼如下:
-
<Window.Resources>
-
<Style TargetType="CheckBox">
-
<Style.Triggers>
-
<Trigger Property="IsChecked" Value="True">
-
<Setter Property="FontSize" Value="20"/>
-
<Setter Property="Foreground" Value="Orange"/>
-
</Trigger>
-
</Style.Triggers>
-
</Style>
-
</Window.Resources>
-
-
<StackPanel>
-
<CheckBox Content="111" Margin="5"/>
-
<CheckBox Content="222" Margin="5"/>
-
<CheckBox Content="333" Margin="5"/>
-
</StackPanel>
2、MultiTrigger
MultiTrigger比Trigger多了一個Conditions屬性,需要同時成立的條件就儲存在這個集合中。
讓我們稍微改動一下上面的例子,要求同時滿足CheckBox被選中且Content為“333”時才會被觸發。
XAML代碼如下:
-
<Window.Resources>
-
<Style TargetType="CheckBox">
-
<Style.Triggers>
-
<MultiTrigger>
-
<MultiTrigger.Conditions>
-
<Condition Property="IsChecked" Value="true"/>
-
<Condition Property="Content" Value="333"/>
-
</MultiTrigger.Conditions>
-
<MultiTrigger.Setters>
-
<Setter Property="FontSize" Value="20"/>
-
<Setter Property="Foreground" Value="Orange"/>
-
</MultiTrigger.Setters>
-
</MultiTrigger>
-
</Style.Triggers>
-
</Style>
-
</Window.Resources>
-
-
<StackPanel>
-
<CheckBox Content="111" Margin="5"/>
-
<CheckBox Content="222" Margin="5"/>
-
<CheckBox Content="333" Margin="5"/>
-
</StackPanel>
3、數據觸發的DataTrigger
程序中經常會遇到基於數據執行某些判斷情況,遇到這種情況時我們可以考慮使用DataTrigger。
DataTrigger對象的Binding屬性會把數據源不斷送過來,一旦送了的值與Value屬性一致,DataTrigger即被觸發。
下面例子中,當TextBox的Text長度小於7個字符時其Border會保持紅色。
XAML代碼如下:
-
<Window x:Class="WpfApplication2.MainWindow"
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-
xmlns:local="clr-namespace:WpfApplication2"
-
Title="MainWindow" Height="350" Width="525">
-
<Window.Resources>
-
<local:L2BConverter x:Key="cvtr"/>
-
<Style TargetType="TextBox">
-
<Style.Triggers>
-
<DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self},
-
Path=Text.Length,
-
Converter={StaticResource cvtr}}" Value="false">
-
<Setter Property="BorderBrush" Value="Red"/>
-
<Setter Property="BorderThickness" Value="1"/>
-
</DataTrigger>
-
</Style.Triggers>
-
</Style>
-
</Window.Resources>
-
-
<StackPanel>
-
<TextBox Margin="5"/>
-
<TextBox Margin="5,0"/>
-
<TextBox Margin="5"/>
-
</StackPanel>
-
</Window>
-
public class L2BConverter:IValueConverter
-
{
-
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
-
{
-
int textLength = (int)value;
-
return textLength > 6 ? true : false;
-
}
-
-
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
-
{
-
throw new NotImplementedException();
-
}
-
}
4、多條件觸發的MultiDataTrigger
有時候我們會遇到要求多個數據條件同時滿足時才能觸發變化的需求,此時可以考慮使用MultiDataTrigger。
比如有這樣一個需求:用戶界面使用ListBox顯示了一列Student數據,當Student對象同時滿足ID為2、Name為Tom的時候,條目就高亮顯示,
XAML代碼如下:
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.Windows;
-
using System.Windows.Controls;
-
using System.Windows.Data;
-
using System.Windows.Documents;
-
using System.Windows.Input;
-
using System.Windows.Media;
-
using System.Windows.Media.Imaging;
-
using System.Windows.Navigation;
-
using System.Windows.Shapes;
-
-
namespace WpfApplication2
-
{
-
/// <summary>
-
/// MainWindow.xaml 的交互邏輯
-
/// </summary>
-
public partial class MainWindow : Window
-
{
-
public MainWindow()
-
{
-
InitializeComponent();
-
List<Student> stuList = new List<Student>(){
-
new Student(){ID="1",Name="Peter",Age=25},
-
new Student(){ID="2",Name="Tom",Age=27},
-
new Student(){ID="3",Name="Ben",Age=20}
-
};
-
-
this.listBoxStudent.ItemsSource = stuList;
-
}
-
}
-
-
/// <summary>
-
/// 學生類
-
/// </summary>
-
public class Student
-
{
-
public string ID { get; set; }
-
public string Name { get; set; }
-
public int Age { get; set; }
-
}
-
}
-
<Window x:Class="WpfApplication2.MainWindow"
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-
Title="MainWindow" Height="350" Width="525">
-
<Window.Resources>
-
<Style TargetType="ListBoxItem">
-
<!--使用Style設置DataTemplate-->
-
<Setter Property="ContentTemplate">
-
<Setter.Value>
-
<DataTemplate>
-
<StackPanel Orientation="Horizontal">
-
<TextBlock Text="{Binding ID}" Width="60"/>
-
<TextBlock Text="{Binding Name}" Width="120"/>
-
<TextBlock Text="{Binding Age}" Width="60"/>
-
</StackPanel>
-
</DataTemplate>
-
</Setter.Value>
-
</Setter>
-
<!--MultiDataTrigger-->
-
<Style.Triggers>
-
<MultiDataTrigger>
-
<MultiDataTrigger.Conditions>
-
<Condition Binding="{Binding Path=ID}" Value="2"/>
-
<Condition Binding="{Binding Path=Name}" Value="Tom"/>
-
</MultiDataTrigger.Conditions>
-
<MultiDataTrigger.Setters>
-
<Setter Property="Background" Value="Orange"/>
-
</MultiDataTrigger.Setters>
-
</MultiDataTrigger>
-
</Style.Triggers>
-
</Style>
-
</Window.Resources>
-
-
<StackPanel>
-
<ListBox x:Name="listBoxStudent" Margin="5"/>
-
</StackPanel>
-
</Window>
效果:
5、由事件觸發的EventTrigger
EventTrigger是觸發器中最特殊的一個。首先,它不是由屬性值或數據的變化來觸發而是由事件來觸發;其次,被觸發后它並非應用一組Setter,而是執行一段動畫。
因此,UI層的動畫效果往往與EventTrigger相關聯。
在下面這個例子中創建了一個針對Button的Style,這個Style包含兩個EventTrigger,一個由MouseEnter事件觸發,另一個由MouseLeave事件觸發。
鼠標進入按鈕時變大,離開時恢復原樣。
XAML代碼如下:
-
<Window.Resources>
-
<Style TargetType="Button">
-
<Style.Triggers>
-
<!--鼠標進入-->
-
<EventTrigger RoutedEvent="MouseEnter">
-
<BeginStoryboard>
-
<Storyboard>
-
<DoubleAnimation To="150" Duration="0:0:0.2" Storyboard.TargetProperty="Width"/>
-
<DoubleAnimation To="150" Duration="0:0:0.2" Storyboard.TargetProperty="Height"/>
-
</Storyboard>
-
</BeginStoryboard>
-
</EventTrigger>
-
<!--鼠標離開-->
-
<EventTrigger RoutedEvent="MouseLeave">
-
<BeginStoryboard>
-
<Storyboard>
-
<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Width"/>
-
<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Height"/>
-
</Storyboard>
-
</BeginStoryboard>
-
</EventTrigger>
-
</Style.Triggers>
-
</Style>
-
</Window.Resources>
-
-
<Canvas>
-
<Button Width="40" Height="40" Content="OK"/>
-
</Canvas>