制作圆角文本框与圆角按钮

决定控件外观的是ControlTemplate,它决定了控件“长成什么样子”,是控件的外衣。因此,可以通过设计ControlTemplate,制作
圆角文本框与圆角按钮。
<Window x:Class="ArtStudio.TemplateDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TemplateDemo" Height="300" Width="300">
    <Window.Resources>
        <ControlTemplate  x:Key="CornerButton" TargetType="{x:Type Button}">
            <Border BorderBrush="Green" BorderThickness="1" CornerRadius="10" Background="Red">
                <ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Border>
        </ControlTemplate>
        <ControlTemplate  x:Key="CornerTextBox" TargetType="{x:Type TextBox}">
            <Border BorderBrush="Blue" BorderThickness="1" CornerRadius="10" Background="#FFD5F0D4">
                <ScrollViewer x:Name="PART_ContentHost" VerticalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </Window.Resources>
    <Canvas>
        <Button x:Name="btnOK" Content="OK" Template="{StaticResource CornerButton}" Height="26" Canvas.Top="175" Width="61" Canvas.Left="58" Click="btnOK_Click"></Button>
        <Button x:Name="btnCancel" Content="Cancel" Template="{StaticResource CornerButton}" Height="26" Canvas.Top="175" Width="61" Canvas.Left="138"></Button>
        <TextBox Template="{StaticResource CornerTextBox}" Canvas.Left="74" Canvas.Top="50" Height="23" Name="textBox1" Width="120" Background="#FF7FBC7D" />
        <TextBox Template="{StaticResource CornerTextBox}" Canvas.Left="74" Canvas.Top="108" Height="23" Name="textBox2" Width="120" />
    </Canvas>
</Window>

效果如图:

posted @ 2013-08-27 14:06  zhouhb  阅读(3784)  评论(2编辑  收藏  举报