在XAML中更改BorderThickness

时间:2015-01-09 14:39:02

标签: c# wpf xaml styles border

我是WPF和XAML的新手,我正在尝试创建自定义按钮样式。

我已经有了一个按钮模板:

<Style x:Key="RoundButtonTemplate" TargetType="Button">
    <Setter Property="Background" Value="LightBlue"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="Test" CornerRadius="5" Background="{TemplateBinding Background}"
                            BorderThickness="1" BorderBrush="Blue">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center">
                    </ContentPresenter>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

一个小小的动画盘旋按钮:

<Style x:Key="Animation" TargetType="Button" BasedOn="{StaticResource RoundButtonTemplate}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <ThicknessAnimation 
                            Storyboard.TargetProperty="BorderThickness"
                            Duration="0:0:0.400"
                            From="1, 1, 1, 1"
                            To="3, 3, 3, 3"/>
                        <DoubleAnimation
                            Storyboard.TargetProperty="Height"
                            Duration="0:0:0.300"
                            From="22"
                            To="25"/>
                        <DoubleAnimation
                            Storyboard.TargetProperty="Width"
                            Duration="0:0:0.300"
                            From="75"
                            To="78"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </Style>

除了ThicknessAnimation之外,一切正常。它怎么会起作用?

1 个答案:

答案 0 :(得分:1)

ControlTemplate中使用BorderThickness="1"替换BorderThickness="{TemplateBinding BorderThickness}"Style BorderThickness内未使用的ControlTemplate动画控件Border使用固定值。

<Style x:Key="RoundButtonTemplate" TargetType="Button">
   <Setter Property="Background" Value="LightBlue"/>
   <Setter Property="BorderThickness" Value="1"/>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="Button">
            <Border 
               x:Name="Test" 
               CornerRadius="5" 
               Background="{TemplateBinding Background}" 
               BorderThickness="{TemplateBinding BorderThickness}" 
               BorderBrush="Blue">
               <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

您还需要添加Setter默认值,例如Background。您也可以考虑使用BorderBrush做同样的事情。稍后,您可以在不更改BorderBrush

的情况下影响控件的Template