How to Add a Scrollbar to Window in C#

Shamim Hafiz picture Shamim Hafiz · May 20, 2011 · Viewed 52.8k times · Source

I have created a window as follows:

Window myWindow = new Window();

How can I add a Vertical Scroll Bar to this Windows and make the Scroll Bar only visible if the Height isn't large enough to show all the elements.

Answer

Osiris76 picture Osiris76 · May 20, 2011

You could add a ScrollViewer element to your window and put the necessary controls into the ScrollViewer control.

<ScrollViewer VerticalScrollBarVisibility="Auto">
    ...
</ScrollViewer>

Or if you want to code it in the code-behind file you could write

ScrollViewer viewer = new ScrollViewer();
viewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
// append scroll viewer to window