WPF zoom canvas and maintain scroll position

Posted by Alex McBride on Stack Overflow See other posts from Stack Overflow or by Alex McBride
Published on 2010-03-14T17:18:31Z Indexed on 2010/03/14 17:25 UTC
Read the original article Hit count: 838

Filed under:
|
|

I have a Canvas element, contained within a ScrollViewer, which I'm zooming using ScaleTransform. However, I want to be able to keep the scroll position of the viewer focused on the same part of the canvas after the zoom operation has finished. Currently when I zoom the canvas the scroll position of the viewer stays where it was and the place the user was viewing is lost.

I'm still learning WPF, and I've been going backwards and forwards a bit on this, but I can't figure out a nice XAML based way to accomplish what I want. Any help in this matter would be greatly appreciated and would aid me in my learning process.

Here is the kind of code I'm using...

<Grid>
    <ScrollViewer Name="TrackScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Canvas Width="2560" Height="2560" Name="TrackCanvas">
            <Canvas.LayoutTransform>
                <ScaleTransform ScaleX="{Binding ElementName=ZoomSlider, Path=Value}" 
                                ScaleY="{Binding ElementName=ZoomSlider, Path=Value}"/>
            </Canvas.LayoutTransform>

            <!-- Some complex geometry describing a motor racing circuit -->

        </Canvas>
    </ScrollViewer>
    <StackPanel Orientation="Horizontal" Margin="8" VerticalAlignment="Top" HorizontalAlignment="Left">
        <Slider Name="ZoomSlider" Width="80" Minimum="0.1" Maximum="10" Value="1"/>
        <TextBlock Margin="4,0,0,0" VerticalAlignment="Center" Text="{Binding ElementName=ZoomSlider, Path=Value, StringFormat=F1}"/>
    </StackPanel>
</Grid>

© Stack Overflow or respective owner

Related posts about wpf

Related posts about canvas