【发布时间】:2011-11-24 09:44:44
【问题描述】:
我正在使用以下按钮样式,它去掉了边框并制作了透明背景来制作我的图像按钮:
<Style x:Key="EmptyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#00000000"/>
<Setter Property="BorderBrush" Value="#00000000"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
样式来自以下答案: WPF button with image of a circle
现在的问题是,无论实际按钮有多大,可点击区域都仅限于图像:
我的按钮代码:
<Button Name="n02" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Style="{DynamicResource EmptyButtonStyle}" Canvas.Left="308" Canvas.Top="157" Height="106" Width="120">
<Image Source="boto_face_off.PNG" Height="61" Width="59" />
</Button>
问题:如何让整个按钮区域对点击做出反应?我想让它保持透明且没有边界,就像现在一样。
【问题讨论】:
-
我不太确定您关于“这是整个按钮区域”的断言是否正确。我怀疑那个盒子是你画布的边界——而不是你的功能按钮的边界。在你的按钮 Style 上放置一个临时的红色边框,看看它是否有你认为的边框。
-
@Balam 无论颜色和粗细如何,边框都不会显示。但是我看到了设计器上的按钮区域,如果我从样式中删除“模板”,边框就会出现,整个区域都可以点击,正如预期的那样。模板所做的事情就是搞砸了。
-
我会删除所有的 setter 属性,然后一次添加一个,看看其中一个是否破坏了它。我不明白为什么你不能显示边框。我只是将 Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" 用于没有边框的按钮。
标签: wpf button user-controls styles wpf-controls