3

This textbox never lets me scroll. I am fairly certain it's because it is vertically expanding to "show" all the text. However, it is expanding past (and underneath) the bottom of the grid row it's in, so the text is not being displayed - yet the scroll bars are disabled (because it thinks there is no reason to show them).

The result is I see the top of the text in the file, and it gets cut off when it reaches the bottom of the grid cell it's in.

What do I need to do to tell the control: "Expand to the width and height of the grid cell you're in, and show the vertical scroll bar when the text won't fit in that space"?

<StackPanel Grid.Column="1" Grid.Row="1">
    <ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Visible">
    <TextBlock x:Name="longText" Foreground="White" FontFamily="Times New Roman" FontSize="24" TextWrapping="Wrap" />
    </ScrollViewer>
</StackPanel>
Dan Homola
  • 3,819
  • 1
  • 28
  • 43
DrDamnit
  • 4,736
  • 4
  • 23
  • 38

3 Answers3

2

Why do you need a StackPanel? I suggest you remove it and place the Grid.Column and Grid.Row in the ScrollViewer:

<ScrollViewer Grid.Column="1" Grid.Row="1"  CanContentScroll="True" VerticalScrollBarVisibility="Visible">
        <TextBlock x:Name="longText" Foreground="White" FontFamily="Times New Roman" FontSize="24" TextWrapping="Wrap" />
</ScrollViewer>
Bugs
  • 4,491
  • 9
  • 32
  • 41
user2837961
  • 1,505
  • 3
  • 27
  • 67
2

If you change it to this it will work.

<ScrollViewer Grid.Column="1" Grid.Row="1" VerticalScrollBarVisibility="Auto">
    <StackPanel>
        <TextBlock x:Name="longText" Foreground="White" FontFamily="Times New Roman" FontSize="24" TextWrapping="Wrap" />
     </StackPanel>
</ScrollViewer>

But in same time you have to make sure that your grids height is set to * or a fixed size:

<Grid>
   <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/> //Or whatever size you want
      <RowDefinition Height="*"/> 
    </Grid.RowDefinitions>

<ScrollViewer Grid.Column="1" Grid.Row="1" VerticalScrollBarVisibility="Auto">
    <StackPanel>
        <TextBlock x:Name="longText" Foreground="White" FontFamily="Times New Roman" FontSize="24" TextWrapping="Wrap" />
     </StackPanel>
</ScrollViewer>

</Grid>

Btw, your StackPanel is redundant unless you have more controls inside it.

EDIT: Well... First of all like mm8 and user2837961 explained a Scrollviewer doesn't make sense inside a StackPanel, because StackPanel can get expanded infinite. ScrollViewer only works when the object inside it are bigger than the size if it self. By giving your Grid row the size of *, you assign the remaining space of the grid for that row, means that items belonging to that row gets a fixed size. And if your Textbox is bigger than the ScrollViewer size the ScrollBarVisibility will be triggered.

Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40
  • It's works! Is it because the grid.row and grid.column did _not_ belong to the textbox, but instead, were in the superflous stack panel? – DrDamnit Feb 03 '17 at 14:36
1

Putting ScrollViewers inside StackPanels is a bad idea. This is because a StackPanel measures its children with infinite horizontal space if its Orientation property is set to Horizontal and infinite vertical space if it is set to Vertical. Please refer to my answer here for more information about this:

Horizontal scroll for stackpanel doesn't work

This basically means that the ScrollViewer element has an infinite height here and thus no vertical scrollbar is displayed.

So, as suggested by @user2837961, you should simply get rid of the StackPanel:

<ScrollViewer Grid.Column="1" Grid.Row="1" VerticalScrollBarVisibility="Visible">
    <TextBlock x:Name="longText" Foreground="White" FontFamily="Times New Roman" FontSize="24" TextWrapping="Wrap" />
</ScrollViewer>

Also make sure that there is no other StackPanel further up the visual tree.

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88