WPF 在鼠标悬停时绘制边框

发布于 2024-09-05 08:48:34 字数 1165 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫言歌 2024-09-12 08:48:34

问题在于 BorderBrush 属性的本地值优先于 Style Trigger这篇 MSDN 文章介绍了如何解析有效值。基本上,从 Border 元素中删除本地值,它应该可以工作。如果需要指定属性,可以在 StyleSetter 中执行此操作。此外,不需要第二个 Trigger ,因为当属性切换回 false 时,该值将恢复为原始值:

<Border 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>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>

The problem is that your local value for the BorderBrush property is taking precedence over your Style Trigger. This MSDN article describes how the effective value is resolved. Basically, remove the local value from the Border element, and it should work. If you need to specify the property, you can do so in a Setter in the Style. Also, the second Trigger is not needed, as the value will revert to the original value when the property switches back to false:

<Border 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>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文