首页 > 解决方案 > WPF Grid 向右对齐并覆盖整列

问题描述

我有一个包含两列的网格,首先,有一个画布,其次,有一个由一些按钮组成的网格,依此类推。我想让第二列中的网格成为一个可打开的菜单,这样,当单击按钮时,它会移动到右侧并缩小自身。问题是我无法强制网格覆盖整个列并一次向右对齐。

当我将网格向右对齐时,可打开的菜单可以正常工作,但是该列仅被网格的一半占用,因此画布和列之间存在不想要的空间。

<Grid x:Name ="GridMenu" HorizontalAlignment="Right" Grid.Column="1">

仅占列的一半

另一方面,当对齐为默认(或拉伸)时

<Grid x:Name ="GridMenu" HorizontalAlignment="Stretch" Grid.Column="1">,

然后触发后

<Window.Resources>
        <Storyboard x:Key="MenuOpen">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridMenu">
                <EasingDoubleKeyFrame KeyTime="0" Value="70"></EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="200"></EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="MenuClose">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="GridMenu">
                <EasingDoubleKeyFrame KeyTime="0" Value="200"></EasingDoubleKeyFrame>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="70"></EasingDoubleKeyFrame>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>

    <Window.Triggers>
        <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="OpenMenuButton">
            <BeginStoryboard Storyboard="{StaticResource MenuOpen}"></BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="CloseMenuButton">
            <BeginStoryboard Storyboard="{StaticResource MenuClose}"></BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>

(EasingDoubleKeyFrame 的值只是临时的)将第一列中的网格移动到中心而不是将其移动到右侧。 网格对齐到中心而不是右侧

标签: c#wpfxamltriggersgrid

解决方案


推荐阅读