eriksmith200
eriksmith200

Reputation: 2179

Silverlight 3 missing ScrollViewer.ScrollChanged event workaround?

I want to be notified of changes to the VerticalOffset of the vertical scrollbar of a ScrollViewer. In WPF there is a ScrollViewer.ScrollChanged event, but in Silverlight 3 this is missing. Does anyone know a workaround?

Upvotes: 9

Views: 3574

Answers (3)

Devper
Devper

Reputation: 11

here is a good link which i found while googling, it also has some sample code which i haven't checked out.

http://dotplusnet.blogspot.com/2010/05/scrollviewer-scroll-change-event-in.html

Upvotes: 1

Chris S
Chris S

Reputation: 65476

There's an easier solution that featured on the silverlight forums:

protected override Size ArrangeOverride(Size finalSize)
{    
    // Assumes you only have one scrollviewer (e.g. fullscreen ScrollViewer)
    var scrollbar = LayoutRoot.GetVisualDescendants()
        .OfType<ScrollBar>()
        .FirstOrDefault();

    if (scrollbar != null)
        scrollbar.Scroll += ScrollBarScroll;

    return base.ArrangeOverride(finalSize);
}

private void ScrollBarScroll(object sender, ScrollEventArgs e)
{

}

Upvotes: 3

AnthonyWJones
AnthonyWJones

Reputation: 189555

You can use element binding, here is a daft example:-

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="60" />
    </Grid.RowDefinitions>
    <ScrollViewer x:Name="ScrollSource">
        <StackPanel>
            <TextBlock>Hello</TextBlock>
            <TextBlock>World</TextBlock>
            <TextBlock>Yasso</TextBlock>
            <TextBlock>Kosmos</TextBlock>
        </StackPanel>
    </ScrollViewer>
    <TextBox Grid.Column="1" Text="{Binding VerticalOffset, ElementName=ScrollSource}" />

</Grid>

As the ScrollViewer is scrolled the Text property of the TextBox is advised of the new value.

Upvotes: 6

Related Questions