首页 文章

WPF:网格中的ScrollViewer

提问于
浏览
13

我有一个网格:

<Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="*"/>
</Grid.RowDefinitions>

第二行是scrollviewer:

<ScrollViewer VerticalScrollBarVisibility="Auto" MinHeight="400" Grid.Row="1">
            <ItemsControl ItemsSource="{Binding SelectedUserControls}"/>
    </ScrollViewer>

如果需要,我希望第二行使用滚动,但滚动是永远不可见的,如果项目控件比屏幕大,则为事件 .

如何在需要时显示滚动?

2 回答

  • 4

    编辑:

    尝试删除'MinHeight = 400',我打赌它有效!!

    您的ItemsControl为400的MinHeight . 所以,除非您有足够的物品占用所有400,否则您将看不到滚动条 . 我猜测容器拿着你的网格(或网格上的显式高度小于400),你有足够的项目对于那个容器来说太大了,但没有足够的项目来填充你的ItemsControl的MinHeight .

    原答案:我刚刚运行了一个包含30个项目的测试应用程序(足以填满MinHeight),它似乎工作正常:

    <Window x:Class="TestApp11.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:l="clr-namespace:TestApp11"
      Title="Window1" Loaded="Window_Loaded" Height="600" Width="800">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="100"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ScrollViewer VerticalScrollBarVisibility="Auto" MinHeight="400" Grid.Row="1">
                <ItemsControl>
                    ...
                     <ListBoxItem Content="Item 30" />
                    ...
                </ItemsControl>
            </ScrollViewer>
        </Grid>
    </Window>
    

    容纳网格的容器是否有明确的高度?

  • 2

    将MinHeight更改为MaxHeight .

相关问题