ScrollViewer's ScrollBar Doesn't Appear

RogerSchlueter-7899 1,236 Reputation points
2023-04-21T01:00:51.7533333+00:00

There are two very simple wpf Windows involved in this problem. The first is a UserControl which contains just one TreeView wrapped in a ScrollViewer:

<UserControl
    x:Class="Categories"
    ....
    <ScrollViewer
        HorizontalScrollBarVisibility="Disabled"
        VerticalScrollBarVisibility="Auto">
        <TreeView
            x:Name="trvCategories"
            ....
        </TreeView>
    </ScrollViewer>
</UserControl>

I use this UserControl in three Windows; in two it works just fine but in the third the vertical ScrollBar does not appear. Here is the XAML:

<Window>
    ....
    xmlns:self="clr-namespace:PIM"
    <StackPanel>
        <ToolBarTray>
            <ToolBar>
                <Button
                    Click="ExitMe"
                    Content="Exit"
                    FontWeight="SemiBold" />
            </ToolBar>
        </ToolBarTray>
        <self:Categories />   <---- Here is the UserControl
    </StackPanel>
</Window>

I've tried numerous ideas but none work. for example, setting

    VerticalScrollBarVisibility="Visioble"

in the UserControl shows a disabled ScrollBar. Setting the SizeToContent property of the containing Window just makes the Window as high as the TreeView no matter which value I set it to. Putting the UserControl inside a Grid on the containing Window makes no difference. Binding the Height or MaxHeight of the ScrollViewer to the ActualHeight of the containing Window makes the TreeView disappear entirely. Neither the suggested answers here or in a wider, independent search have helped. What am I missing?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,686 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
771 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 41,331 Reputation points Microsoft Vendor
    2023-04-25T03:26:12.28+00:00

    Hi,@Roger Schlueter. If you don't set the height, it won't show the ScrollBar because the height is enough. You set up VerticalScrollBarVisibility = "Auo" in the UserControl. It will display the VerticalScrollBar based on the height of the control. I set the height to 100 in the window and it will display the following.

     <self:Categories Height="100" />
    

    User's image

    User's image

    When I set VerticalScrollBarVisibility="Visible" in UserControl, the result is shown as follows. <self:Categories />

    User's image

    If the response is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful