wpf 如果内容适合,则禁用 ScrollViewer VerticalScrollBarVisible

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23043920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:31:36  来源:igfitidea点击:

Disable ScrollViewer VerticalScrollBarVisible if content fits

wpfxamlwindows-phone-8

提问by Knowleech

I am currently writing my very first Windows Phone (8) App which also is my very first Xaml Application. So it is likely I just did not find the solution for my problem on my own, because I don't know which words to feed google. I tried, but found nothing useful. I found that one, but it does not help:

我目前正在编写我的第一个 Windows Phone (8) 应用程序,这也是我的第一个 Xaml 应用程序。所以很可能我只是没有自己找到解决我的问题的方法,因为我不知道要向 google 输入哪些词。我试过了,但没有发现任何有用的东西。我找到了那个,但它没有帮助:

How to disable "scroll compression" in ScrollViewer

如何在 ScrollViewer 中禁用“滚动压缩”

Here is the important part of my XAML:

这是我的 XAML 的重要部分:

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <StackPanel VerticalAlignment="Top">
         <TextBlock x:Name="InfoText" TextWrapping="Wrap" VerticalAlignment="Top" Text="VersionInfoText"/>
    </StackPanel>
</ScrollViewer>

I will programmatically change the content of my TextBlock InfoText. The text might be short enough to fit in completely, or it might be rather long. That is why I embedded it into a ScrollViewer. (By the way, there will be further Controls added to the StackPanel later.)

我将以编程方式更改 TextBlock 的内容InfoText。文本可能足够短以完全适合,也可能相当长。这就是我将它嵌入到 ScrollViewer 的原因。(顺便说一句,稍后会向 StackPanel 添加更多控件。)

The ScrollViewer produces these "overbounce" effects if it cannot scroll any further. That is nice if the text is large, but when there is nothing to scroll I don't want this effect to be visilbe.

如果 ScrollViewer 无法进一步滚动,则会产生这些“过度反弹”效果。如果文本很大,那很好,但是当没有任何内容可以滚动时,我不希望这种效果变得可见。

I tried VerticelScrollBarVisibility="Disable", which successfully disables the effect. Now my question:

我试过VerticelScrollBarVisibility="Disable",它成功地禁用了效果。现在我的问题:

Can I automatically (by XAML-Magic) switch between Autoand Disabledepending on the Height of my StackPanel and the Hight of my ScrollViewer?

我可以自动(通过XAML魔法)之间切换Auto,并Disable根据我的StackPanel和我的ScrollViewer的海特的高度?

I was hoping Autowould do the trick, but it does not (tested in the VS2013 Emulator WVGA).

我希望Auto能做到这一点,但它没有(在 VS2013 仿真器 WVGA 中测试)。

回答by Brett

In VS2013 setting VerticalScrollBarVisibility="Auto"worked for me.

在 VS2013 设置中VerticalScrollBarVisibility="Auto"对我有用。

回答by Preetesh

Try adding this attribute to your ScrollViewer

尝试将此属性添加到您的 ScrollViewer

VerticalScrollMode="Auto"

Also try disabling the HorizontalScrollModeand HorizontalScrollBarVisiblityattributes.

还可以尝试禁用HorizontalScrollModeHorizontalScrollBarVisiblity属性。

Let me know if this doesn't work. I will then have to make a sample app to see if I can make that work for you. Right now I am just guessing. Try it.

如果这不起作用,请告诉我。然后我将不得不制作一个示例应用程序,看看我是否可以为你工作。现在我只是猜测。尝试一下。

回答by Pradeep Kesharwani

You could dynamically set the SetVerticalScrollBarVisibility to Disabled depends on your InfoText length in your cs code...

您可以将 SetVerticalScrollBarVisibility 动态设置为 Disabled 取决于您的 cs 代码中的 InfoText 长度...

if(InfoText.Length() >n)
{
    ScrollViewer.SetVerticalScrollBarVisibility(scrollViewer, ScrollBarVisibility.Auto);
}
else
{
   ScrollViewer.SetVerticalScrollBarVisibility(scrollViewer, ScrollBarVisibility.Disabled);
}

回答by ROMAN

You can check if TextBlockheight is greater than the heightof the ScrollViewer.

您可以检查是否TextBlock高度比大heightScrollViewer

In xaml:

在 xaml 中:

        <ScrollViewer x:Name="TestScrollViewer">

            <TextBlock x:Name="InfoText"
                       Text="Information"
                       TextWrapping="Wrap"
                       VerticalAlignment="Top" />

        </ScrollViewer>

In cs:

在 cs 中:

    public MainPage()
    {
        InitializeComponent();

        Loaded += (sender, args) =>
        {
            TestScrollViewer.IsEnabled = InfoText.ActualHeight > TestScrollViewer.ActualHeight;

            // OR

            TestScrollViewer.VerticalScrollBarVisibility = InfoText.ActualHeight > TestScrollViewer.ActualHeight
                ? ScrollBarVisibility.Visible
                : ScrollBarVisibility.Disabled;
        };
    }