【问题标题】:WPF: ScrollViewer in gridWPF:网格中的 ScrollViewer
【发布时间】:2011-03-09 15:36:44
【问题描述】:

我有一个网格:

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

第二行是scrollviewer:

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

如果需要,我希望第二行带有滚动条, 但是如果项目控件大于屏幕,则滚动永远不可见。

如何在需要时显示滚动条?

【问题讨论】:

    标签: c# wpf grid scrollviewer


    【解决方案1】:

    编辑:

    尝试删除“MinHeight=400”,我敢打赌它会起作用!!

    您的 ItemsControl 上的 MinHeight 为 400。因此,除非您有足够的项目来占用所有 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。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多