【发布时间】:2011-04-24 06:34:29
【问题描述】:
我在 .png 中有一个彩色圆圈,其余的都是透明的。 我可以将该图像放在按钮上,但按钮样式的其余部分在图像的透明区域上可见。如何在正常、鼠标悬停和按下模式下隐藏它?
【问题讨论】:
我在 .png 中有一个彩色圆圈,其余的都是透明的。 我可以将该图像放在按钮上,但按钮样式的其余部分在图像的透明区域上可见。如何在正常、鼠标悬停和按下模式下隐藏它?
【问题讨论】:
试试这种风格
<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>
并按如下方式使用
<Button Style="{DynamicResource EmptyButtonStyle}">
<Image Source="/Assets/circle.png" />
</Button>
【讨论】:
听起来您当前正在使用现有按钮的模板,并且只是在其中添加了一个图像。您需要做的是修改按钮的<Template> 本身,以便它使用您的图像而不是默认模板。
如果您在 Google 上搜索“WPF 模板”或“WPF 圆形按钮”之类的内容,您可能会找到一些很好的示例。
【讨论】:
我最终使用了最简单的解决方案(可能不是最优雅的)。
<Image x:Name="nextImage" MouseDown="nextButton_Click"></Image>
【讨论】: