WPF:网格中的ScrollViewer

Sho*_*oki 13 c# wpf grid scrollviewer

我有一个网格:

<Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="*"/>
</Grid.RowDefinitions>
Run Code Online (Sandbox Code Playgroud)

第二行是scrollviewer:

    <ScrollViewer VerticalScrollBarVisibility="Auto" MinHeight="400" Grid.Row="1">
            <ItemsControl ItemsSource="{Binding SelectedUserControls}"/>
    </ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

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

如何在需要时显示滚动?

Sco*_*ott 5

编辑:

尝试删除 '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>
Run Code Online (Sandbox Code Playgroud)

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