WPF 在鼠标悬停时绘制边框
我有一个 WPF 应用程序,我正在尝试制作一个类似图像的关闭按钮。 我正在尝试绘制边框或在鼠标悬停时显示边框...但我似乎无法使其工作。 我尝试过 6 种不同的方法......图像、图像边框、画笔等。
我目前正在使用以下代码:
<Canvas Name="cMin" Height="16" Width="16"
Grid.Column="1" Grid.Row="1">
<Canvas.Background>
<ImageBrush ImageSource="_.png" Stretch="None" />
</Canvas.Background>
<Border BorderBrush="Transparent" BorderThickness="1" Background="Transparent"
CornerRadius="0" Height="18" Width="18">
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="LightBlue" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</Canvas>
任何帮助将不胜感激, 谢谢!
I have a WPF Application where I am trying to make a close button like image.
I am trying to draw a border or have a border show up on mouse over... But I cannot seem to make it work.
I have tried like 6 different methods of doing so...Images, borders with images, brushes, ect.
I am using the following code at the moment:
<Canvas Name="cMin" Height="16" Width="16"
Grid.Column="1" Grid.Row="1">
<Canvas.Background>
<ImageBrush ImageSource="_.png" Stretch="None" />
</Canvas.Background>
<Border BorderBrush="Transparent" BorderThickness="1" Background="Transparent"
CornerRadius="0" Height="18" Width="18">
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="LightBlue" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</Canvas>
Any help would be appreciated,
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于
BorderBrush
属性的本地值优先于Style
Trigger
。 这篇 MSDN 文章介绍了如何解析有效值。基本上,从Border
元素中删除本地值,它应该可以工作。如果需要指定属性,可以在Style
的Setter
中执行此操作。此外,不需要第二个 Trigger ,因为当属性切换回 false 时,该值将恢复为原始值:The problem is that your local value for the
BorderBrush
property is taking precedence over yourStyle
Trigger
. This MSDN article describes how the effective value is resolved. Basically, remove the local value from theBorder
element, and it should work. If you need to specify the property, you can do so in aSetter
in theStyle
. Also, the secondTrigger
is not needed, as the value will revert to the original value when the property switches back to false: