1. 程式人生 > >WPF Button控制元件模板

WPF Button控制元件模板

<Window x:Class="ControlTemplateDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
            <Border Name="border" BorderBrush="Yellow" Background="Red" BorderThickness="2">
                <ContentPresenter Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center" TextBlock.Foreground="White"></ContentPresenter>
               
            </Border>

            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter TargetName="border" Property="Background" Value="Green"></Setter>
                </Trigger>
                <Trigger Property="IsPressed" Value="true">
                    <Setter TargetName="border" Property="Background" Value="Blue"></Setter>
                </Trigger>
            </ControlTemplate.Triggers>

        </ControlTemplate>
    </Window.Resources>
    <StackPanel Margin="5">
        <Button Margin="5">normal</Button>
        <Button Margin="5" Template="{StaticResource ButtonTemplate}">Template</Button>
    </StackPanel>
</Window>