使 WPF 网格在鼠标进入窗口时向上滑动

本文关键字:窗口 网格 WPF 鼠标 | 更新日期: 2023-09-27 18:36:15

我目前有XAML:

<Grid Name="WindowGrid">
    <Grid Height="66" HorizontalAlignment="Stretch" Margin="0" Name="ControlsGrid" VerticalAlignment="Bottom" Background="#B4000000" />
</Grid>

我想要的是当鼠标进入窗口(甚至最好是 WindowGrid)时,ControlGrid 向上滑动,反之亦然(当光标离开时,它会向下滑动)。我在WPF/XAML方面的经验很少,但在WinForms方面经验最多。我知道有一种方法可以使用故事板/触发器来做到这一点,但我看到的例子太混乱了。

使 WPF 网格在鼠标进入窗口时向上滑动

使用事件触发器执行此操作:

<Grid Name="WindowGrid">
    <Grid.Triggers>
        <EventTrigger RoutedEvent="MouseEnter">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation 
                        Storyboard.TargetName="ControlsGrid" 
                        Storyboard.TargetProperty="(Grid.Height)"
                        From="0" 
                        To="66" 
                        Duration="0:0:0.5" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="MouseLeave">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation 
                        Storyboard.TargetName="ControlsGrid"
                        Storyboard.TargetProperty="(Grid.Height)"
                        From="66" 
                        To="0" 
                        Duration="0:0:0.5" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>
    <!-- 
        This is a sample content to demostrate animation;
        Without it 'WindowGrid' will be collapsed.
    -->
    <ListBox />
    <Grid Margin="0" Name="ControlsGrid" VerticalAlignment="Bottom" Background="#B4000000" />
</Grid>