生命 Window.Resources 樣式定義
子節點Style
屬性 TargetType 指定應用該樣式的控件類型(如:Button)
屬性:x:Key 定義樣式的名字
屬性:BaseOn 可以集成其他的style樣式
字節點 Setter
屬性 Property 值指定樣式屬性
屬性Value 值 指定樣式屬性值
具體控件使用方式:
Style="{StaticResource 樣式style名}"
小節示例Demo:
<Window x:Class="WpfApp1.example1" 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:WpfApp1" mc:Ignorable="d" Title="example1" Height="450" Width="800"> <Window.Resources> <Style x:Key="btn1" TargetType="Button"> <Setter Property="FontSize" Value="25"/> <Setter Property="Background" Value="Pink"/> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="80px" /> <RowDefinition Height="100px"/> <RowDefinition/> </Grid.RowDefinitions> <Border Background="#66ccff" /> <TextBlock FontSize="25px" TextAlignment="Center" VerticalAlignment="Center" Text="洛天依天鈿智能數據平臺" Foreground="#fff" /> <Grid Grid.Row="1"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Button Margin="5" Grid.Row="0" Grid.Column="0" Style="{StaticResource btn1}" TextBlock.Foreground="#fff">手動導入</Button> <Button Margin="5" Grid.Row="0" Grid.Column="1" Style="{StaticResource btn1}" TextBlock.Foreground="Green">開啓線程</Button> <Button Margin="5" Grid.Row="0" Grid.Column="2" Style="{StaticResource btn1}" TextBlock.Foreground="red">關閉所有</Button> <Button Margin="5" Grid.Row="0" Grid.Column="3" Style="{StaticResource btn1}" TextBlock.Foreground="#fff">同步站點</Button> <Button Margin="5" Grid.Row="0" Grid.Column="4" Style="{StaticResource btn1}" TextBlock.Foreground="#fff">數據通知</Button> <Button Margin="5" Grid.Row="0" Grid.Column="5" Style="{StaticResource btn1}" TextBlock.Foreground="#fff">日誌查詢</Button> </Grid> </Grid> </Window>