0

So I'm trying to make a window that can have a horizontal or vertical scroll bar, the catch is the top row of the grid should be frozen and place and not able to be scrolled vertically (much like frozen panes in excel). The horizontal scroll bar should scroll both panes. I have it mostly working, here is a subset of code that demonstrates.

<ScrollViewer HorizontalScrollBarVisibility="Auto"  VerticalScrollBarVisibility="Disabled">
    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

        <TextBlock Text="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" />
        <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" >
            <TextBlock Text="test2">
                <Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" />
                <Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" />
                <Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" />
                <Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" /><Run Text="&#10;test2" />
            </TextBlock>
        </ScrollViewer>
    </Grid>
</ScrollViewer>

So this almost works correctly, the screen can scroll horizontally and scroll both panes, and scrolling vertically scrolls only the bottom pane (which is what I want). However the vertical scroll bar does not appear on the side of the window, instead you have to scroll all the way to the right in order to use it. Is there any way to cause the scroll bar to snap to the window and scroll vertically without causing it to also scroll the top pane?

Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138

1 Answers1

1

The first ScrollViewer is telling the Grid: "You have as many space as you want, feel free to grow as needed". Because of this the inner ScrollViewer never gets triggered because it has no limits in space.

try to remove that first ScrollViewer.

---- EDIT -------

So, both "panels" will have the same width? Well.. wrap each panel in its own ScrollViewer. The top panel should have its horizontal ScroolViewer "hidden" and no vertical one. The bottom should have both visible or "Auto". Sync the top ScrollViewer (in code) when the bottom changes (check http://perezgb.com/2009/07/08/how-to-keep-two-scrollviewers-in-sync-in-wpf)

NestorArturo
  • 2,476
  • 1
  • 18
  • 21
  • Both scroll bars do get triggered, the problem is to see the vertical one you have to scroll all the way to the right. Removing the first scroll viewer just removes one of the scroll viewers. – Kevin DiTraglia Aug 29 '12 at 18:45
  • The second scroll appears way to the right because the first scroll is telling "You have as many space horizontally as you need... etc". You might try to wrap your TextBlock with its own ScrollViewer and remove the first one. – NestorArturo Aug 29 '12 at 18:50
  • I see that, is there something I can do to get the behavior I am trying to get? – Kevin DiTraglia Aug 29 '12 at 18:53
  • The problem with that is now I'm only scrolling the text box horizontally, I want to scroll the whole screen horizontally, but only the bottom of the grid vertically. – Kevin DiTraglia Aug 29 '12 at 18:57
  • So, you need the horizontal scroll bar at the bottom of the screen, but it should have no effect on the second rows of the grid? – NestorArturo Aug 29 '12 at 19:07
  • I edited the question to try to make it a bit more clear. The horizontal scroll bar will scroll both panes, however the vertical scroll bar should only scroll the bottom pane (so they will be in sync horizontally, but the top pane can never be scrolled out of view vertically). It is working as intended as is (you can dump the code and see), but the vertical scroll bar doesn't attach to the window, so you need to scroll to the right to see it. – Kevin DiTraglia Aug 29 '12 at 19:11
  • Well.. strange arrangement. It is not impossible, but now I see it's hard (for me). Tough about sync two ScrollViewers horizontally and hide one of them so the visible "moves" the hidden, however, both containers must have same width for them to sync nicely. Got the problem? – NestorArturo Aug 29 '12 at 19:23
  • They will be the same width, however if it is longer than the width of the window the vertical scroll bar gets pushed to the side – Kevin DiTraglia Aug 29 '12 at 19:25