如何“冻结”鼠标悬停时的 UI(主窗口)

发布于 2024-12-12 20:31:18 字数 509 浏览 1 评论 0原文

我想要实现的是,当鼠标悬停在主窗口上时,所有 UI 元素都应该冻结,我认为可以通过将 Window.IsEnabled 设置为 false 来完成,并且在鼠标离开主窗口后,一切都应该是恢复正常。

我尝试在样式定位窗口中定义属性触发器,但它不起作用。代码很简单,

<Style.Triggers>
    <Trigger Property="Window.IsMouseOver" Value="True">
        <Setter Property="Window.IsEnabled" Value="false"/>
    </Trigger>
</Style.Triggers>

事实上这种属性触发器在Grid上也不起作用。有人可以做出一些解释吗?

我还尝试在 Window 上显式使用 MouseEnter 和 MouseLeave 事件,并在处理程序中设置禁用/启用逻辑。这有效。我想知道是否可以在 XAML 中执行此操作?

What I want to achieve is that when the mouse is hovering over the main window, all the UI elements should freeze, which I think can be done by setting Window.IsEnabled to false, and after the mouse leaves the main window, everything should be back to normal.

I've tried to define a property trigger in a style targetting Window, but it doesn't work. The code is as lollow,

<Style.Triggers>
    <Trigger Property="Window.IsMouseOver" Value="True">
        <Setter Property="Window.IsEnabled" Value="false"/>
    </Trigger>
</Style.Triggers>

In fact this kind of property trigger wouldn't work on Grid either. Can anyone make some explanations?

I also tried to explicitly use the MouseEnter and MouseLeave events on Window, and set the disable/enable logic in the handlers. This works. I wonder if it's possible to do this in XAML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

↘紸啶 2024-12-19 20:31:19

老实说,我不知道为什么你的代码不起作用,我认为它会发生某种冲突,但我不知道为什么

无论如何,你可以使用 eventsetter 在 XAML 中完成它,它不是那么优雅,但它可以工作

<Window.Triggers>
    <EventTrigger RoutedEvent="Window.MouseEnter">
        <BeginStoryboard>
            <Storyboard Name="sb">
                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled" >
                    <BooleanKeyFrameCollection>
                        <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:0:1"></DiscreteBooleanKeyFrame>
                    </BooleanKeyFrameCollection>
                </BooleanAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers> 

Well to be honest I don't know why your code doesn't work, I think it goes in some kind of conflict but I don't know why

Anyway you can do it in XAML using eventsetter, It's not so elegant but it works

<Window.Triggers>
    <EventTrigger RoutedEvent="Window.MouseEnter">
        <BeginStoryboard>
            <Storyboard Name="sb">
                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled" >
                    <BooleanKeyFrameCollection>
                        <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:0:1"></DiscreteBooleanKeyFrame>
                    </BooleanKeyFrameCollection>
                </BooleanAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文