KimhillZhang  

简单使用控件模板

<Window.Resources>
        <ControlTemplate x:Key="buttontemplate" TargetType="{x:Type Button}">
            <Border BorderBrush="Orange" BorderThickness="{TemplateBinding Button.BorderThickness}" CornerRadius="3"
                    Background="Red" TextBlock.Foreground="Black" Margin="30">
                <ContentPresenter RecognizesAccessKey="True" HorizontalAlignment="Center"
                                   VerticalAlignment="Center" Content="{TemplateBinding Button.Content}"></ContentPresenter>
            </Border>
        </ControlTemplate>
    </Window.Resources>
    <StackPanel>
        <Button FontSize="15" Template="{StaticResource ResourceKey=buttontemplate}" BorderThickness="10" Content="Hello"></Button>
        <Button FontSize="15" Template="{StaticResource ResourceKey=buttontemplate}" BorderThickness="4" Content="您好"></Button>
    </StackPanel>

 

控件模板触发器

<Window.Resources>
        <ControlTemplate x:Key="buttontemplate" TargetType="{x:Type Button}">
            <Border Name="border" BorderBrush="Orange" BorderThickness="{TemplateBinding Button.BorderThickness}" CornerRadius="3"
                    Background="Red" TextBlock.Foreground="Black" Margin="30">
                <StackPanel>
                        <Rectangle Name="FocusCue" Visibility="Hidden" Stroke="Black"
                                    StrokeThickness="3" StrokeDashArray="1 2" SnapsToDevicePixels="True"></Rectangle>
                 
                    <ContentPresenter RecognizesAccessKey="True" HorizontalAlignment="Center"
                                       VerticalAlignment="Center" Content="{TemplateBinding Button.Content}">
                   
                    </ContentPresenter>
                </StackPanel>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter TargetName="border" Property="Background" Value="White"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="FocusCue" Property="Visibility" Value="Visible"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
       
    </Window.Resources>
    <StackPanel>
        <Button Template="{StaticResource ResourceKey=buttontemplate}" Content="Hello"/>
    </StackPanel>

 

posted on 2012-03-28 23:33  KimhillZhang  阅读(196)  评论(0编辑  收藏  举报