How to 'Freeze' the UI (main window) when mouse is hovering over

682 views Asked by At

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?

1

There are 1 answers

0
MaRuf On

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>