CogentP
CogentP

Reputation: 259

WPF ScrollViewer not activating on Window Resize

This may have been asked, but wasn't able to find the exact question.

Basically I have a WPF Window that I use as a Form. Now for the form, I have a StackPanel that helps keep all the controls and labels in place.

If the user has a smaller resolution display, the window size will be slower so for example 800x600. Some controls get lost.

For this I have added a ScrollViewer wrapped around the StackPanel. But the ScrollViewer never activates. Its probably got something to do with the stackpanel never being limited I suppose. But how can I activate the scroll viewer if the user resizes the window, or the window (when it opens) cannot display all contents properly?

I don't think its necesary to put my xaml here, but if you need it let me know. thanks!

Upvotes: 2

Views: 4291

Answers (3)

Matt R
Matt R

Reputation: 2617

I ran into a similar problem where the horizontal scrollbar wasn't being displayed even though there was content on the screen that was hidden.

<Window x:Class="WPFTestingPlatform.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="{Binding SystemParameters.PrimaryScreenHeight}" 
        Width="{Binding SystemParameters.PrimaryScreenWidth}"
        ResizeMode="CanResizeWithGrip">
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <StackPanel Width="1248" Height="600">
                <TextBlock HorizontalAlignment="Right">Right</TextBlock>
        </StackPanel>
    </ScrollViewer>
</Window>

Set the window to have the size you would like for your window. Also set the Stackpanel size so it neatly fits all of your content.

The window size isn't as important as setting the StackPanel height/width otherwise the StackPanel will inherit it's size from the window.

You can resize this window and the scrollbars will appear/disappear (if ResizeMode="CanResizeWithGrip" is set). If you do not set HorizontalScrollBarVisibility, the scrollviewer will not display the horizontal scrollbar regardless of the content size.

Upvotes: 0

Rocky
Rocky

Reputation: 4524

This may help you

<StackPanel Orientation="Vertical">
  <ScrollViewer Name="scrollViewer1">
   <DataGrid Name="dgDataList" ItemsSource="{Binding}">
    <DataGrid.Columns>
       <DataGridTemplateColumn Header="View" IsReadOnly="True">
           <DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
                  //control like textblock, image etc
                </DataTemplate>
           </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>                                    
    </DataGrid.Columns>
 </DataGrid>
</ScrollViewer>
<ScrollViewer Name="scrollViewer2" >
 <DataGrid Name="dgDataList2" ItemsSource="{Binding}" >
  <DataGrid.Columns>
       <DataGridTemplateColumn Header="View" IsReadOnly="True">
           <DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
                  //control like textblock, image etc
                </DataTemplate>
           </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>                                    
    </DataGrid.Columns>
 </DataGrid>
</ScrollViewer>
</StackPanel>

Upvotes: 0

Dean Chalk
Dean Chalk

Reputation: 20451

A StackPanel has infinite size (doesn't respect its parent bounds) so you should wrap it in a Grid, which in turn is inside the ScrollViewer.

Upvotes: 4

Related Questions