如何在WPF xaml中按下按钮时突出显示按钮

时间:2014-12-18 07:48:39

标签: wpf wpf-controls

我在Styles.xaml文件中有两个按钮样式,如MyStyleButton和MyStyleButtonClick。

我将MyStyleButton应用于现有按钮,如下所示

            

当按下按钮时,我想申请" MyStyleButtonClick"在xaml页面中使用触发器来显示这些按钮之间的区别。

1 个答案:

答案 0 :(得分:0)

这是一个" GlassButton"风格,按下时有不同的行为,在事件中触发" IsPressed":

<Style x:Key="GlassButton" TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="42" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="{x:Type Button}">
      <Border x:Name="ButtonBorder" 
              CornerRadius="25,25,25,25" 
              BorderThickness="4,4,4,4" 
              Background="#AA000000"  
              BorderBrush="#99FFFFFF"
              RenderTransformOrigin="0.5,0.5">
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="1.7*"/>
          </Grid.RowDefinitions>
          <Border Grid.Row="0" CornerRadius="23,23,0,0">
            <Border.Background>
              <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Color="#08FFFFFF" Offset="0"/>
                <GradientStop Color="#88FFFFFF" Offset="1"/>
              </LinearGradientBrush>
            </Border.Background>
          </Border>
          <ContentPresenter x:Name="ButtonContentPresenter"
                            VerticalAlignment="Center"  
                            Grid.RowSpan="2" 
                            HorizontalAlignment="Center"/>
        </Grid>
      </Border>
      <ControlTemplate.Triggers>
        <Trigger Property="IsPressed" Value="True">
          <Setter Property="RenderTransform" TargetName="ButtonBorder">
            <Setter.Value>
              <TransformGroup>
                <ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
              </TransformGroup>
            </Setter.Value>
          </Setter>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Setter.Value>
</Setter>

具有此样式的Button将按如下方式编码:

<Button Style="{DynamicResource GlassButton}" 
        Grid.Column="1" 
        Margin="0,0,18.577,17.148" 
        Name="button1" 
        Height="91.456" 
        VerticalAlignment="Bottom" 
        HorizontalAlignment="Right" 
        Width="207.764">Button</Button>