【问题标题】:WPF button with custom template can only be pressed on content带有自定义模板的 WPF 按钮只能在内容上按下
【发布时间】:2013-03-08 13:59:39
【问题描述】:

我有一个按钮样式+模板如下:

<Style x:Key="ButtonStyle" TargetType="RepeatButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="RepeatButton">
                <Border Background="{DynamicResource {x:Static SystemColors.ControlColor}}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">            
                    <Path Data="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}">
                        <Path.Fill>
                            <SolidColorBrush x:Name="PathBrush" Color="{x:Static SystemColors.ControlDarkColor}" />
                        </Path.Fill>
                    </Path>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

按钮是这样使用的:

<RepeatButton Style="{StaticResource ButtonStyle}" Content="M 0 4 L 8 4 L 4 0 Z" />

但是,只有当鼠标悬停在路径上时才能按下按钮,而不是整个按钮。当鼠标在按钮上但不在路径上时,如何让按钮被按下?

【问题讨论】:

    标签: .net wpf button


    【解决方案1】:

    这是因为您的ControlTemplateBorder 元素的背景属性设置不正确。它需要Brush,而您提供了Color。由于它是 DynamicResource,所以根本没有设置画笔。所以它对 Click 不可见。只需提供刷子。

    这应该可以解决问题:)

    <Border Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 2012-07-16
      • 2013-04-06
      • 2015-11-10
      • 2010-11-16
      相关资源
      最近更新 更多