2011年8月9日 星期二

Expression Blend(36)-Control Style與套用

一個Style可以套用到不同的Control,以下定義一個Style到用到Label與Button
  • 加一個Label
image
  • 建立Style
image
  • 設myStyle
image
  • 設Background與Foreground,然後回MainWondow
image
目前XAML看起來如下
<Window x:Class="WpfApplication9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="300">
    <Window.Resources>
        <Style x:Key="myStyle" TargetType="{x:Type Label}">
            <Setter Property="Background" Value="#FF060AF5"/>
            <Setter Property="Foreground" Value="#FFF7F1F1"/>
        </Style>
    </Window.Resources>
    <Grid>
        <Label x:Name="label" Content="Label" Height="48" Margin="136,24,60,0" VerticalAlignment="Top" Style="{DynamicResource myStyle}"/>       
     </Grid>
</Window>
  • 加一個Label並套用
image
  • 修改Style ,移除TargetType
<Window.Resources>
        <Style x:Key="myStyle" >             <Setter Property="Control.Background" Value="#FF060AF5"/>
            <Setter Property="Control.Foreground" Value="#FFF7F1F1"/>
        </Style>
    </Window.Resources>
  • 加一個Button,Button可以套用到相同的Style
image

沒有留言:

總網頁瀏覽量