Kevin DiTraglia
Kevin DiTraglia

Reputation: 26068

ScrollViewer doesn't snap to window

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?

Upvotes: 0

Views: 317

Answers (1)

NestorArturo
NestorArturo

Reputation: 2516

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)

Upvotes: 1

Related Questions