1. 程式人生 > >WPF中的Style樣式設定

WPF中的Style樣式設定

1、使用Style設定控制元件的某些屬性,並在指定的範圍內起作用:
下面是樣式定義:

<Window.Resources>
    <Style TargetType="Button">
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="FontSize" Value="25"/>
        <Setter Property="FontWeight" Value="Heavy"/>
    </Style>
</Window.Resources>

該樣式的定義是作為一種資源被儲存下來,對整個窗體中的所有Button起作用:

<Window x:Class="StyleDemo.MainWindow"
    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:StyleDemo"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style TargetType="Button">
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="FontSize" Value="25"/>
        <Setter Property="FontWeight" Value="Heavy"/>
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Button Grid.Row="0" Grid.Column="0" Content="button1" Margin="5"/>
    <Button Grid.Row="0" Grid.Column="1" Content="button2" Margin="5"/>
</Grid>
</Window>	

2、定義一種Style樣式只對指定的Button物件起作用,為Style新增一個x:Key=“ButtonStyle”
樣式定義:

<Window.Resources>
    <Style TargetType="Button" x:Key="ButtonStyle">
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="FontSize" Value="25"/>
        <Setter Property="FontWeight" Value="Heavy"/>
    </Style>
</Window.Resources>

此種定義方式,通過ButtonStyle只對指定的控制元件設定樣式

<Window x:Class="StyleDemo.MainWindow"
    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:StyleDemo"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style TargetType="Button" x:Key="ButtonStyle">
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="FontSize" Value="25"/>
        <Setter Property="FontWeight" Value="Heavy"/>
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Button Grid.Row="0" Grid.Column="0" Content="button1" Margin="5"/>
    <Button Style="{StaticResource ButtonStyle}" Grid.Row="0" Grid.Column="1" Content="button2" Margin="5"/>
</Grid>
</Window>	

3、定義一種Style可以對外界的互動做出響應,比如滑鼠按下時字型顏色變化,釋放滑鼠顏色恢復。通過在Style中新增觸發器Trigger設定;另外可以使用BaseOn“繼承”其它的Style;
參考程式碼:

<Window.Resources>
    <Style TargetType="Button" x:Key="ButtonStyle">
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="FontSize" Value="25"/>
        <Setter Property="FontWeight" Value="Heavy"/>
    </Style>

    <Style TargetType="Button" x:Key="TriggerButton" BasedOn="{StaticResource ButtonStyle}">
        <Style.Triggers>
            <Trigger Property="IsPressed" Value="true">
                <Setter Property="Foreground" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>