Question

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?

Was it helpful?

Solution 2

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

OTHER TIPS

 WPF Layout Controls - ScrollViewer

Most of the time,we don't know about the size of control ,which will be displayed in scree. Also there is case of making product resolution dependent as this is basic property of wpf. So That’s why in WPF there is a control i.e. ScrollViewer(Attached property) which can enable scrolling of those content whenever the content goes out to the display area.

<ScrollViewer HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible">
        <Grid ScrollViewer.HorizontalScrollBarVisibility="Visible">
            <Grid RenderTransformOrigin="0.555,0.491">
                <Grid.RowDefinitions>

                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>

                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition ></ColumnDefinition>
                    <ColumnDefinition ></ColumnDefinition>
                    <ColumnDefinition ></ColumnDefinition>
                    <ColumnDefinition ></ColumnDefinition>
                    <ColumnDefinition ></ColumnDefinition>
                    <ColumnDefinition ></ColumnDefinition>
                    <ColumnDefinition ></ColumnDefinition>
                    <ColumnDefinition ></ColumnDefinition>
                </Grid.ColumnDefinitions>
    </Grid>
    </ScrollViewer>


Above example is a implementation of horizontal and vertical  scrollbar in a wpf form.

Automatic vertical scroll bar in WPF TextBlock?

For Detail Please Visit : http://wpfbugs.blogspot.in/2014/02/wpf-layout-controls-scrollviewer.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top