“自定义”按钮上的“单击”效果

时间:2017-07-06 09:36:23

标签: c# wpf button

我正在互联网上寻找箭形按钮。我找到了这个解决方案:

 <Button HorizontalAlignment="Center" Click="Button_Click">
         <Button.Template>
             <ControlTemplate TargetType="Button">
                 <Grid>
                     <StackPanel Orientation="Horizontal">
                         <Rectangle Width="100" Height="25" Stroke="Orange" Fill="Orange" VerticalAlignment="Center"/>
                         <Polygon  Points= "0,0 30,25, 0,50" Stroke="Orange" Fill="Orange" VerticalAlignment="Center"/>
                     </StackPanel>
                     <ContentPresenter HorizontalAlignment="Left" Margin="45,0,0,0" VerticalAlignment="Center" Content="Login"/>
                 </Grid>
             </ControlTemplate>
         </Button.Template>
 </Button>

这个问题是,虽然这是一个可以点击的按钮,但我“丢失”按钮点击效果(即按下按钮时的效果)。你知道有没有办法解决我的问题?

1 个答案:

答案 0 :(得分:1)

您需要在自定义模板中自己定义此效果。

添加IsPressed触发器,用于更改模板中元素的某些属性:

<Button.Template>
    <ControlTemplate TargetType="Button">
        <Grid>
            <StackPanel Orientation="Horizontal">
                <Rectangle x:Name="rect" Width="100" Height="25" Stroke="Orange" Fill="Orange" VerticalAlignment="Center"/>
                <Polygon x:Name="pol"  Points= "0,0 30,25, 0,50" Stroke="Orange" Fill="Orange" VerticalAlignment="Center"/>
            </StackPanel>
            <ContentPresenter HorizontalAlignment="Left" Margin="45,0,0,0" VerticalAlignment="Center" Content="Login"/>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsPressed" Value="true">
                <Setter Property="Fill" TargetName="rect" Value="Red"/>
                <Setter Property="Fill" TargetName="pol" Value="Blue"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Button.Template>