如何使一个按钮形状在WPF自定义路径? [英] How do I make a button shape a custom Path in WPF?

查看:672
本文介绍了如何使一个按钮形状在WPF自定义路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用的ControlTemplate下方的按钮。

I have a button using the ControlTemplate below.

<ControlTemplate x:Key="ControlTemplate" TargetType="{x:Type Button}">
        <Grid>
            <Path x:Name="ButtonPath" Fill="{Binding IsSelected, Converter={StaticResource ArrowBackgroundColorConverter}, UpdateSourceTrigger=PropertyChanged}"  
                  Data="{Binding Converter={StaticResource ArrowPathSelector}}" Stretch="UniformToFill" Margin="0 0 -35 0"></Path>
            <TextBlock Grid.Column="0" Grid.Row="0" FontFamily="{StaticResource ApplicationFont}" FontSize="{StaticResource Heading3}" HorizontalAlignment="Center" Margin="35 0 0 0" VerticalAlignment="Center" Text="{Binding Title}" Foreground="White"/>
        </Grid>
    </ControlTemplate>

但是当我点击按钮在我的应用程序是不是勾勒路径,而是原始的按钮。

but when I click the Button in my app it is not outlining the Path but instead the original Button.

我不能完全弄清楚如何获得按钮来反映路径本身。任何帮助是极大AP preciated,谢谢!

I cant quite figure out how to get the button to reflect the Path itself. Any help is greatly appreciated, Thanks!

推荐答案

如果您想更改焦点矩形的形状(虚线边框指示键盘焦点),你需要创建一个自定义的 FocusVisualStyle ,在相同的形状绘制虚线路径作为你的按钮的内容:

If you want to change the shape of the focus rectangle (the dotted border indicating keyboard focus), you need to create a custom FocusVisualStyle that draws a dotted path in the same shape as your button content:

<Style x:Key="ButtonFocusRectangle" TargetType="Control">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate>
        <Path Stroke="Black"
              StrokeDashArray="2 2"  
              Data="M 0,0 50,50 100,0 Z"
              Stretch="UniformToFill"
              Margin="0 0 -35 0" />
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

然后,在按钮,设置 FocusVisualStyle ={StaticResource的ButtonFocusRectangle}。请注意,它很可能是更清洁的自定义一个风格的按钮,并将它同时应用模板 FocusVisualStyle 为您服务。

Then, on the button, set FocusVisualStyle="{StaticResource ButtonFocusRectangle}". Note that it would probably be cleaner to define a custom Style for the button, and have it apply both the Template and FocusVisualStyle for you.

另外,你可以得到完全通过设置 FocusVisualStyle ={NULL X}摆脱了视觉焦点的。您可能会选择这样做,只需在普通的模板通过,例如,改变路径的笔触一个 IsKeyboardFocused 触发。

Alternatively, you can just get rid of the focus visual entirely by setting FocusVisualStyle="{x:Null}". You might opt to do this, and simply draw a focus hint in your regular Template by, for example, changing the path's stroke with an IsKeyboardFocused trigger.

顺便说一句,你可能要设置均匀在两个模板的路径。 UniformToFill 可引起削波。

As an aside, you probably want to set Stretch to Uniform on the paths in both templates. UniformToFill can cause clipping.

这篇关于如何使一个按钮形状在WPF自定义路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆