如何使滚动查看器在 WPF 中将高度设置为自动? [英] How to make scrollviewer work with Height set to Auto in WPF?

查看:58
本文介绍了如何使滚动查看器在 WPF 中将高度设置为自动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,如果 ScrollViewer 所在的网格行的高度设置为 Auto,垂直滚动条将不会生效,因为实际ScrollViewer 的大小可以大于视线高度.所以为了使滚动条工作,我应该将高度设置为固定数字或星形高度

I have learned that if the height of a grid row, where the ScrollViewer resides, is set as Auto, the vertical scroll bar will not take effect since the actual size of the ScrollViewer can be larger than the height in sight. So in order to make the scroll bar work, I should set the height to either a fixed number or star height

但是,我现在有这个要求,我有两个不同的视图驻留在两个网格行中,并且我有一个切换按钮可以在这两个视图之间切换:当一个视图显示时,另一个隐藏/消失.所以我定义了两行,两个高度都设置为Auto.我将每一行中视图的可见性绑定到我的 ViewModel 的一个布尔属性(一个从 True 转换为 Visible,另一个从 Truecode> 到 Collapsed.这个想法是当一个视图的可见性为 Collapsed 时,网格行/视图的高度将自动更改为 0.

However, I now have this requirement, that I have two different views reside in two grid rows, and I have a toggle button to switch between these two views: when one view is shown, the other one is hidden/disappeared. So I have defined two rows, both heights are set as Auto. And I bind the visibility of the view in each row to a boolean property from my ViewModel (one is converted from True to Visible and the other from True to Collapsed. The idea is when one view's visibility is Collapsed, the height of the grid row/view will be changed to 0 automatically.

视图显示/隐藏工作正常.但是,在一个视图中,我有一个 ScrollViewer,正如我提到的,当行高设置为 Auto 时,它不起作用.谁能告诉我如何在让 ScrollViewer 自动工作的同时满足这样的要求?我想我可以在代码隐藏中设置高度.但由于我使用的是 MVVM,因此需要额外的通信/通知.有没有更直接的方法来做到这一点?

The view show/hidden is working fine. However, in one view I have a ScrollViewer, which as I mentioned doesn't work when the row height is set as Auto. Can anybody tell me how I can fulfill such requirement while still having the ScrollViewer working automatically`? I guess I can set the height in code-behind. But since I am using MVVM, it would require extra communication/notification. Is there a more straightforward way to do that?

推荐答案

如果可以,将高度从 Auto 更改为 *.

Change Height from Auto to *, if you can.

示例:

    <Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="200" Width="525">
    <StackPanel Orientation="Horizontal"  Background="LightGray">

        <Grid Width="100">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <ScrollViewer VerticalScrollBarVisibility="Auto" x:Name="_scroll1">
                <Border Height="300" Background="Red" />
            </ScrollViewer>
            <TextBlock Text="{Binding ElementName=_scroll1, Path=ActualHeight}" Grid.Row="1"/>
        </Grid>

        <Grid Width="100">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
                <ScrollViewer VerticalScrollBarVisibility="Auto" x:Name="_scroll2">
                    <Border Height="300" Background="Green" />
                </ScrollViewer>
            <TextBlock Text="{Binding ElementName=_scroll2, Path=ActualHeight}" Grid.Row="1"/>
        </Grid>
    </StackPanel>
</Window>

这篇关于如何使滚动查看器在 WPF 中将高度设置为自动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆