wpf 按下按钮时的“推动”效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27441292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:47:10  来源:igfitidea点击:

"Pushed" effect on button press

wpfxamluser-controlswpf-controls

提问by Mangesh

I have circular button as enter image description here

我有圆形按钮 在此处输入图片说明

XAML for that is,

XAML 就是,

<!--round button-->
<Style x:Key ="roundButtonTemplate" TargetType ="{x:Type Button}">
    <Setter Property ="Margin" Value ="10,0,10,10"/>
    <Setter Property ="Template">
        <Setter.Value>
            <ControlTemplate TargetType ="{x:Type Button}">
                <Grid>
                    <Ellipse x:Name ="OuterRing" Width ="40" Height ="40" Fill="{TemplateBinding Background}"/>
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property ="IsPressed" Value ="True">
                        <Setter TargetName ="OuterRing" Property ="Height" Value ="30"/>
                        <Setter TargetName ="OuterRing" Property ="Width" Value ="30"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And I have applied it as,

我已经将它应用为,

<Button Style="{StaticResource roundButtonTemplate}" Click="button_Click" Background="#CC41B1E1" Width="42" Height="42" Margin="0,0,10,0">
     <Image Height="24" Width="24" Source="/Images/add.png" />
</Button>

Now the problem is when I press the button only the circle shrinks but the image remains as it is. I want the whole button to shrink with image as well to give it a perfect "Pushed" effect. Any idea what is getting wrong?

现在的问题是当我按下按钮时,只有圆圈缩小,但图像保持原样。我希望整个按钮也随着图像缩小,以使其具有完美的“推动”效果。知道出了什么问题吗?

回答by dkozl

Instead of changing size to some fixed value in you template apply ScaleTransformto main Gridof your template

不是将模板中的大小更改为某个固定值,而是应用于模板的ScaleTransform主体Grid

<ControlTemplate TargetType ="{x:Type Button}">
   <Grid RenderTransformOrigin="0.5,0.5" x:Name="RootGrid">
      <Ellipse x:Name ="OuterRing" Width ="40" Height ="40" Fill="{TemplateBinding Background}"/>
      <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
   </Grid>
   <ControlTemplate.Triggers>
      <Trigger Property ="IsPressed" Value ="True">
         <Setter TargetName="RootGrid" Property="RenderTransform">
            <Setter.Value>
               <ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
            </Setter.Value>
         </Setter>
      </Trigger>
   </ControlTemplate.Triggers>
</ControlTemplate>

don't forget to set RenderTransformOrigin="0.5,0.5"

不要忘记设置 RenderTransformOrigin="0.5,0.5"