Search code examples
wpfscrollviewerhorizontal-scrollinghorizontalscrollview

Horizontal Scrollbar for WPF ScrollViewer never displayed


I am having trouble getting a Horizontal Scrollbar to be displayed for a WPF scroll viewer. The ScrollViewer contains a User Control which dynamically renders its contents based on an input object so may be of variable size - when it renders itself it sets the Width and Height of the User Control to be the correct size.

I've set HorizontalScrollBarVisibility to Visible which should force it to always be displayed but it is never displayed. I have also tried playing with enabling/disabling CanContentScroll but it does not seem to make any difference.

Vertical Scroll Bars work fine without issue.

                ScrollViewer scroll = new ScrollViewer();
                scroll.HorizontalContentAlignment = HorizontalAlignment.Left;
                scroll.VerticalContentAlignment = VerticalAlignment.Top;
                scroll.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
                scroll.CanContentScroll = true;
                Grid.SetRow(scroll, 1);

                GalaxyViewer viewer = new GalaxyViewer(g);
                viewer.HorizontalAlignment = HorizontalAlignment.Left;
                viewer.VerticalAlignment = VerticalAlignment.Top;
                viewer.HideShips();
                viewer.IsEditable = this.IsEditable;
                viewer.GalaxyEdited += this._galaxyEditedHandler;
                if (this._activePlayer > 0 && !this.IsEditable) viewer.ShowShipsForPlayer(this._activePlayer);
                scroll.Content = viewer;
                ScrollViewer.SetCanContentScroll(viewer, true);
                ScrollViewer.SetHorizontalScrollBarVisibility(viewer, ScrollBarVisibility.Visible);

                scroll.UpdateLayout();
                grid.Children.Add(scroll);

Anyone have any ideas/solutions for this?


Solution

  • Found the cause of the problem - the scroll bar was being displayed but I was automatically resizing the control containing the scoll viewer as the window was resized and this meant that the horizontal scoll bar was being clipped from the display