WPF:网格中的 ScrollViewer

发布于 2024-10-21 05:04:59 字数 492 浏览 3 评论 0原文

我有一个网格:

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

第二行带有滚动查看器:

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

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

如何让滚动条在需要时出现?

I have a grid:

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

The second row is with scrollviewer:

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

I want the second row to be with scroll if needed,
But the scroll is never visible, event if the items controls are bigger than the screen.

How can i get the scroll to appear when needed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诠释孤独 2024-10-28 05:04:59

编辑:

尝试删除“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>

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

EDIT:

Try removing 'MinHeight=400' and I bet it works!!

You have a MinHeight on your ItemsControl of 400. So until you have enough items to take up all 400, you will not see your scrollbar. I'm guessing the container holding your grid (or the explicit height on your grid is less that 400), and you have enough items to be too big for that container, but not enough items to fill the MinHeight of your ItemsControl.

Original Answer: I just ran a test app with 30 items in it (enough to fill the MinHeight) and it seems to work fine:

<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>

Does your container holding your grid have an explicit Height?

梦幻的心爱 2024-10-28 05:04:59

将最小高度更改为最大高度。

Change MinHeight to MaxHeight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文