ScrollViewer的内容不会扩展

时间:2015-02-19 14:52:42

标签: c# wpf

我有以下窗口

pic

现在,如果我尝试拉下Gridsplitter,我只能将蓝色网格放在可见窗口中。但是当我向下滑动分割器时,我想要一个滚动条出现并能够将其向下拉到底部,直到蓝色网格不再可见为止。

<Window.Content>
    <Grid>        
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition />
            <RowDefinition Height="25" />
        </Grid.RowDefinitions>
        <Menu Name="MainMenu" Grid.Row="0">                
        </Menu>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200" />
                <ColumnDefinition  />
            </Grid.ColumnDefinitions>
            <DockPanel x:Name="Green" Grid.Column="0" Width="Auto" Height="Auto">
                <views:MyView></views:MyView>
            </DockPanel>
            <GridSplitter Grid.Column="0" Width="6"></GridSplitter>
            <Grid Grid.Column="1" >
                <ScrollViewer VerticalScrollBarVisibility="Auto">
                    <Grid VerticalAlignment="Stretch">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="7" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0">
                            <views:ListView x:Name="Yellow" ></views:ListView>
                        </Grid>
                        <GridSplitter Grid.Row="1" Height="7" HorizontalAlignment="Stretch" />
                        <Grid Grid.Row="2" >
                            <ContentControl Content="{Binding LoadedControl}" x:Name="Blue" />
                        </Grid>
                    </Grid>
                </ScrollViewer>
            </Grid>
        </Grid>
        <StatusBar x:Name="StatusBar" Grid.Row="2">                
        </StatusBar>
    </Grid>
</Window.Content>

我需要在这里更改什么?

3 个答案:

答案 0 :(得分:2)

ScrollViewer向下移动,使其包含第2行中的蓝色ContentControl,并确保ContentControl设置了HeightMinHeight

ScrollViewers允许他们的孩子占用他们想要的空间,并且只有当子对象大于ScrollViewer大小时才显示滚动条。

另外作为旁注,您可以删除布局中的一些额外网格,以便于阅读。这是一个示例,其中删除了一堆,第一个被替换为DockPanel:)

<DockPanel>        
    <Menu Name="MainMenu" DockPanel.Dock="Top" Height="25" /> 
    <StatusBar x:Name="StatusBar" DockPanel.Dock="Bottom" Height="25"/>  

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="6" />
            <ColumnDefinition  />
        </Grid.ColumnDefinitions>

        <views:MyView x:Name="Green" Grid.Column="0" />
        <GridSplitter Grid.Column="1" Width="6" />
        <Grid Grid.Column="2" >
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="7" />
                 <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <views:ListView x:Name="Yellow" Grid.Row="0" />
            <GridSplitter Grid.Row="1" Height="7" HorizontalAlignment="Stretch" />
            <ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Auto">
                <ContentControl x:Name="Blue" MinHeight="400"/>
            </ScrollViewer>
        </Grid>
    </Grid>      
</DockPanel>

答案 1 :(得分:1)

希望您找到一个更好的解决方案XD,因为我的使用后面的代码

  

我使用了控件GridSplitter的DragDelta事件,并且放大了网格的高度,因此可以激活ScrollBar

xaml代码:

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200" />
                <ColumnDefinition  />
            </Grid.ColumnDefinitions>
            <DockPanel x:Name="Green" Grid.Column="0" Width="Auto" Height="Auto" Background="#FF0CFA8F" >
                <local:BusyUserControl Width="200" Height="200"/>
            </DockPanel>
            <GridSplitter ResizeDirection="Rows" Grid.Column="0" Width="6"></GridSplitter>
            <ScrollViewer x:Name="MainScrollViewer" VerticalScrollBarVisibility="Auto" Grid.Column="1" >
                <Grid x:Name="MainGrid">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="7" />
                        <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid >
                            <views:ListView x:Name="Yellow" ></views:ListView>
                        </Grid>
                        <GridSplitter  Grid.Row="1" Height="7" ResizeBehavior="PreviousAndNext"  HorizontalAlignment="Stretch" DragDelta="GridSplitter_DragDelta" />
                        <Grid Grid.Row="2">
                            <ContentControl Content="{Binding LoadedControl}" x:Name="Blue" />
                        </Grid>
                </Grid>
            </ScrollViewer>
        </Grid>

代码背后:

    private void GridSplitter_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
    {
        if (e.VerticalChange > 1500 || e.VerticalChange > -15000) return;

        if (e.VerticalChange > 0 || Visibility.Visible.Equals(MainScrollViewer.ComputedVerticalScrollBarVisibility))
        {
            this.MainGrid.Height = this.MainGrid.ActualHeight + e.VerticalChange;
        }
        e.Handled = true;
    }

注意: 当滚动条不再可见时,我停止缩小网格(保持网格拉伸),这是条件的含义

Visibility.Visible.Equals(MainScrollViewer.ComputedVerticalScrollBarVisibility)

希望这可以帮助你和thx提出问题:)

答案 2 :(得分:0)

如果要为分割网格中的每个部分显示垂直滚动条,请尝试以下代码:

<Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition  />
                </Grid.ColumnDefinitions>
                <DockPanel x:Name="Green" Grid.Column="0" Width="Auto" Height="Auto" Background="#FF0CFA8F" >
                    <local:BusyUserControl Width="200" Height="200"/>
                </DockPanel>
                <GridSplitter Grid.Column="0" Width="6"></GridSplitter>
                <Grid Grid.Column="1" >
                    <Grid VerticalAlignment="Stretch">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="7" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="0" >
                            <Grid >
                                <views:ListView x:Name="Yellow" ></views:ListView>
                            </Grid>
                        </ScrollViewer>
                        <GridSplitter Grid.Row="1" Height="7" HorizontalAlignment="Stretch" />
                        <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="2" >
                            <Grid Grid.Row="2" >
                                <ContentControl Content="{Binding LoadedControl}" x:Name="Blue" />
                            </Grid>
                        </ScrollViewer>
                    </Grid>
                </Grid>
            </Grid>

否则澄清你的需要