样式

1、讲到样式,先来对比一下wpf 样式和网页css:

wpf代码:

WPF知识点全攻略12- 样式和资源
<Window
    x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">

    <Window.Resources>
        <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="#4CAF50"></Setter>
            <Setter Property="BorderThickness" Value="0"></Setter>
            <Setter Property="Foreground" Value="White"></Setter>
            <Setter Property="Padding" Value="32,15"></Setter>
            <Setter Property="FontSize" Value="16"></Setter>
            <Setter Property="Margin" Value="4,2"></Setter>
        </Style>
     
    </Window.Resources>

    <StackPanel Margin="10">
        <TextBlock Text="WPF 按钮" FontSize="24" FontWeight="Bold"></TextBlock>
        <StackPanel Orientation="Horizontal" Margin="0,20" >
            <Button  HorizontalAlignment="Left" VerticalAlignment="Center"  Content="默认按钮" />
            <Button  HorizontalAlignment="Left" VerticalAlignment="Center"  Content="按钮" Style="{StaticResource ButtonStyle}" />
        </StackPanel>
    </StackPanel>
</Window>
View Code

相关文章: