如何动态设置WPF ScrollViewer高度?

发布于 2024-08-08 08:08:01 字数 1392 浏览 8 评论 0 原文

我有以下测试示例:

<Window x:Class="WpfScrollTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="200" Width="200">
    <Border>
        <StackPanel>
            <Label Width="Auto" Height="Auto" Content="Text to mess up the scrollview"/>
                <ScrollViewer Height="{Binding RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type Border}}, Path=ActualHeight}">
                    <StackPanel>
                        <Button MinWidth="100" MinHeight="100" Content="Button"/>
                        <Button MinWidth="100" MinHeight="100" Content="Button"/>
                    </StackPanel>
                </ScrollViewer>
            </StackPanel>
    </Border>
</Window>

它创建了这个:

滚动条底部缺失

我的问题是如何动态设置 ScrollViewer.Height 同时仍然能够看到滚动条的底部?在我的示例中,ScrollViewerHeight 太长,因为它上面有 Label ..

我不想修复 <将 ScrollViewer 的 code>Height 设置为静态值。

I have the following test sample:

<Window x:Class="WpfScrollTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="200" Width="200">
    <Border>
        <StackPanel>
            <Label Width="Auto" Height="Auto" Content="Text to mess up the scrollview"/>
                <ScrollViewer Height="{Binding RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type Border}}, Path=ActualHeight}">
                    <StackPanel>
                        <Button MinWidth="100" MinHeight="100" Content="Button"/>
                        <Button MinWidth="100" MinHeight="100" Content="Button"/>
                    </StackPanel>
                </ScrollViewer>
            </StackPanel>
    </Border>
</Window>

Which creates this:

Scrollbar bottom missing

My question is how do I set the ScrollViewer.Height dynamically while still being able to see the bottom of the scrollbar? In my sample, the Height of the ScrollViewer is too long because of the Label above it ..

I don't want to fix the Height of the ScrollViewerto a static value.

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

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

发布评论

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

评论(1

海的爱人是光 2024-08-15 08:08:01

我建议将外部 StackPanel 删除到网格中,因为 Stackpanel 不会考虑子级的大小。并删除 ScrollViewer.Height 绑定。现在您只需为 Grid 创建两个 RowDefinition,并将 Label 设置为 Grid.Row=0,将 ScrollViwer 设置为 Grid.Row=1。

代码如下。所以我的建议是,仅在必要时使用 StackPanel/Canvas,并且可能用于内部级别。尝试更多地使用网格以获得非常动态的布局。

  <Border>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Width="Auto" Height="Auto" Content="Text to mess up the scrollview"/>
        <ScrollViewer Grid.Row="1" >
            <StackPanel>
                <Button MinWidth="100" MinHeight="100" Content="Button"/>
                <Button MinWidth="100" MinHeight="100" Content="Button"/>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Border>

I would recommend to remove the outer StackPanel to a Grid, since Stackpanel wont respect the children size. And remove the ScrollViewer.Height binding. Now you just need to create two RowDefinition for the Grid and place the Label to Grid.Row=0 and ScrollViwer to Grid.Row=1.

Code is below. So my tip here is, use StackPanel/Canvas only if necessary and may be for the inner levels. Try to use Grid more to get very dynamic layouts.

  <Border>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Width="Auto" Height="Auto" Content="Text to mess up the scrollview"/>
        <ScrollViewer Grid.Row="1" >
            <StackPanel>
                <Button MinWidth="100" MinHeight="100" Content="Button"/>
                <Button MinWidth="100" MinHeight="100" Content="Button"/>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Border>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文