2

我的网格中有以下布局

<Grid>
    <Grid.RowDefinitions >
        <RowDefinition Height="50" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Button Click="Button_Click"  Grid.Row="0" Width="50" Grid.Column="2" Content="Test" />
    <Grid Grid.Row="1">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="120" />
        <ColumnDefinition  />
        <ColumnDefinition Width="Auto"  />
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="0" Grid.Row="1" Background="red" />
    <StackPanel Grid.Column="1" Grid.Row="1" Background="Blue" />
    <GridSplitter x:Name="gs"  Grid.Column="1" Grid.Row="1" Width="10" />
    <StackPanel x:Name="green" MinWidth="100" Grid.Column="2" Grid.Row="1" Background="Green" />
   </Grid>
</Grid>

当按下按钮时,我基本上想要这个布局:

   <StackPanel Grid.Column="0" Grid.Row="1" Background="red" />
    <StackPanel Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" Background="Blue" />
    <!--<GridSplitter x:Name="gs"  Grid.Column="1" Grid.Row="1" Width="10" />
    <StackPanel x:Name="green" MinWidth="100" Grid.Column="2" Grid.Row="1" Background="Green" />-->

再次按下按钮时的这种布局:

    <StackPanel Grid.Column="0" Grid.Row="1" Background="red" />
    <StackPanel Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="1" Background="Blue" />
    <GridSplitter x:Name="gs"  Grid.Column="1" Grid.Row="1" Width="10" />
    <StackPanel x:Name="green" MinWidth="100" Grid.Column="2" Grid.Row="1" Background="Green" />

如何使 gridsplitter 和最后一个面板消失,并让中间列中的面板填充它的位置?(反之亦然)有没有办法在运行时更改列跨度?

谢谢!

4

1 回答 1

1

您可以使用触发器,也可以使用单击按钮时触发的情节提要/动画。

你想做这样的事情.....

<Window.Resources>

        <Storyboard x:Key="OnClick1">
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="gs" Storyboard.TargetProperty="(FrameworkElement.Width)">
                <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="green" Storyboard.TargetProperty="(FrameworkElement.MinWidth)">
                <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>

    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button">
            <BeginStoryboard Storyboard="{StaticResource OnClick1}"/>
        </EventTrigger>
    </Window.Triggers>

为了反转这个动画,你应该使用一些东西来保持状态,比如使用 ToggleButton 并使用 ToggleButton.Checked && ToggleButton.Unchecked 路由事件。或者,将依赖属性添加到维护状态的代码隐藏中。

我认为主要问题是记住也要更改 MinWidth 属性。MinWidth 将覆盖实际宽度。

于 2010-01-15T14:17:02.227 回答