WPF:使用GridSplitter隐藏网格行

时间:2011-03-11 10:32:10

标签: c# .net wpf

如何在调整分割器大小后隐藏最后两行? 当最后两行被隐藏时,webBrowser应填充所有区域。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="5" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <WebBrowser Name="webBrowser" />
    <GridSplitter Grid.Row="1" ResizeDirection="Rows" 
                  ResizeBehavior="PreviousAndNext" Width="Auto" Height="5" 
                  HorizontalAlignment="Stretch"
                  Visibility="...">
    </GridSplitter>
    <c:MyControl Grid.Row="2" Visibility="..." />
</Grid>

2 个答案:

答案 0 :(得分:4)

快速解决此问题:

    public class HideableGridSplitter : GridSplitter
    {
        private GridLength height;

        public HideableGridSplitter()
        {
            this.IsVisibleChanged += HideableGridSplitter_IsVisibleChanged;
        }

        void HideableGridSplitter_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            Grid parent = base.Parent as Grid;
            if (parent == null)
                return;
            int rowIndex = Grid.GetRow(this);
            if (rowIndex + 1 >= parent.RowDefinitions.Count)
                return;
            var lastRow = parent.RowDefinitions[rowIndex + 1];

            if (this.Visibility == Visibility.Visible)
                lastRow.Height = height;
            else
            {
                height = lastRow.Height;
                lastRow.Height = new GridLength(0);
            }
        }
    }

答案 1 :(得分:0)

在MyControl.ActualHeight上触发。如果为0,则将可见性设置为已折叠

<DataTrigger Binding="{Binding ElementName=MyControl, Path=ActualHeight}" Value=0>
    <Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>