style 樣式
- 如何定義一個 style 樣式
<Button Content="樣式" Width="100" Height="50"><Button.Style><Style></Style></Button.Style></Button>
- 擁有的屬性
- targetType = “” 針對什么類型生效
- setter 設置屬性的值 property = “屬性名稱” value = “值”
- triggers 觸發器,當命中后觸發什么條件.
- multitriggers 多重條件觸發器
- eventTriggers 事件觸發器,內部都是動畫。
<Button ><Button.Style><Style TargetType="Button"><Style.Setters><Setter Property="Width" Value="100"></Setter><Setter Property="Height" Value="100"></Setter><Setter Property="Content" Value="不好"></Setter><Setter Property="Background" Value="#FFA500"></Setter></Style.Setters><Style.Triggers><!--當鼠標移動上時觸發以下條件--><Trigger Property="IsMouseOver" Value="True"><Setter Property="Content" Value="你好"></Setter></Trigger><MultiTrigger><MultiTrigger.Conditions><!--當鼠標移動上時并且發生點擊的時候觸發以下條件--><Condition Property="IsMouseOver" Value="True"></Condition><Condition Property="IsPressed" Value="True"></Condition></MultiTrigger.Conditions><MultiTrigger.Setters><Setter Property="Foreground" Value="Red"/></MultiTrigger.Setters></MultiTrigger><!--事件觸發器,點擊鼠標右鍵--><EventTrigger RoutedEvent="MouseRightButtonDown"><BeginStoryboard><Storyboard><!--在1秒鐘的事件里,對屬性值進行變更,這里是將寬高變成250--><DoubleAnimation Duration="0:0:1" To="250" Storyboard.TargetProperty="Width"></DoubleAnimation><DoubleAnimation Duration="0:0:1" To="250" Storyboard.TargetProperty="Height"></DoubleAnimation></Storyboard></BeginStoryboard></EventTrigger></Style.Triggers></Style></Button.Style> </Button>
- Style 的復用
如何定義一個資源的使用。<Window x:Class="zhiyu.training.StyleWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:zhiyu.training"mc:Ignorable="d"Title="StyleWindow" Height="450" Width="800"xmlns:sys ="clr-namespace:System;assembly=mscorlib"> <Window.Resources><!--如何定義一個int類型的變量--><sys:Double x:Key="value">123</sys:Double> </Window.Resources> <Grid><Button Width="{StaticResource value}" Height="30" Margin="10" HorizontalAlignment="Left"><Button.Style><Style TargetType="Button"><!--在 style 中定義資源--><Style.Resources><SolidColorBrush Color="Green" x:Key="Brush"></SolidColorBrush></Style.Resources><!-- 引用資源 --><Setter Property="Background" Value="{StaticResource Brush}"></Setter></Style></Button.Style></Button> </Grid>
如何復用一個 style
<Window x:Class="zhiyu.training.StyleWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:zhiyu.training"mc:Ignorable="d"Title="StyleWindow" Height="450" Width="800"xmlns:sys ="clr-namespace:System;assembly=mscorlib"><Window.Resources><!--如何定義一個int類型的變量--><sys:Double x:Key="value">123</sys:Double><Style TargetType="Button"><Style.Resources><SolidColorBrush Color="Green" x:Key="Brush"></SolidColorBrush></Style.Resources><Setter Property="Background" Value="{StaticResource Brush}"></Setter></Style><Style TargetType="Button" x:Key="ab"><Style.Resources><SolidColorBrush Color="Yellow" x:Key="Brush"></SolidColorBrush></Style.Resources><Setter Property="Background" Value="{StaticResource Brush}"></Setter></Style></Window.Resources><Grid><!--全局風格--><Button Width="{StaticResource value}" Height="30" Margin="10" HorizontalAlignment="Left"></Button><!--使用ab風格--><Canvas><Button Style="{StaticResource ab}" Height="30" Width="200" Canvas.Top="50"></Button></Canvas><!--什么風格都不用--><Canvas><Button Style="{x:Null}" Height="30" Width="200" Canvas.Top="100"></Button></Canvas></Button></Grid></Window>
如何繼承一個 Style BaseOn
<Window.Resources><Style TargetType="Button" x:Key="baseOn"><Setter Property="Content" Value="你好"></Setter></Style><!-- 使用 BaseOn 繼承--><Style TargetType="Button" x:Key="ab" BasedOn="{StaticResource baseOn}"><Style.Resources><SolidColorBrush Color="Yellow" x:Key="Brush"></SolidColorBrush></Style.Resources><Setter Property="Background" Value="{StaticResource Brush}"></Setter></Style></Window.Resources>
<Grid>
<Canvas><Button Style="{StaticResource ab}" Height="30" Width="200" Canvas.Top="50"></Button>
</Canvas>
</Grid>