Disable ScrollViewer VerticalScrollBarVisible if content fits
Asked Answered
T

4

8

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:

How to disable "scroll compression" in ScrollViewer

Here is the important part of my 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.)

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.

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

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

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

Thunderhead answered 13/4, 2014 at 14:23 Comment(0)
M
19

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

Marianamariand answered 9/5, 2017 at 9:53 Comment(0)
C
4

Try adding this attribute to your ScrollViewer

VerticalScrollMode="Auto"

Also try disabling the HorizontalScrollMode and HorizontalScrollBarVisiblity attributes.

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.

Capers answered 15/4, 2014 at 5:58 Comment(2)
Thanks for the idea. I will try it later this week and I will post my answer.Thunderhead
Using VerticalScrollMode reports the following error in VS2013: The member "VerticalScrollMode" is not recognized or is not accessible."Marianamariand
J
0

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

if(InfoText.Length() >n)
{
    ScrollViewer.SetVerticalScrollBarVisibility(scrollViewer, ScrollBarVisibility.Auto);
}
else
{
   ScrollViewer.SetVerticalScrollBarVisibility(scrollViewer, ScrollBarVisibility.Disabled);
}
Jellied answered 13/4, 2014 at 15:37 Comment(0)
D
0

You can check if TextBlock height is greater than the height of the ScrollViewer.

In xaml:

        <ScrollViewer x:Name="TestScrollViewer">

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

        </ScrollViewer>

In cs:

    public MainPage()
    {
        InitializeComponent();

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

            // OR

            TestScrollViewer.VerticalScrollBarVisibility = InfoText.ActualHeight > TestScrollViewer.ActualHeight
                ? ScrollBarVisibility.Visible
                : ScrollBarVisibility.Disabled;
        };
    }
Danzig answered 13/4, 2014 at 20:6 Comment(1)
Ok. It is a code behind solution then. Would it be possible to formulate something like this entirely in xaml? I have seen some strange xaml before with conditional formatting or something.Thunderhead

© 2022 - 2024 — McMap. All rights reserved.