WPF网格显示/隐藏列

时间:2010-01-15 14:07:52

标签: wpf grid hide show

我的网格中有以下布局

<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" />

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

谢谢!

1 个答案:

答案 0 :(得分: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&amp;&amp; ToggleButton.Unchecked路由事件。或者,将依赖项属性添加到维护状态的代码隐藏中。

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