Getting Viewbox and ScrollViewer to work together

c#, scrollviewer, visual-studio, windows-phone-8, zooming

Solution

I think you should instead look at the viewportcontroller and look into the basic lens sample, where they have implemented this. The viewportcontroller, has a scrollviewer inside, and viewport.

The basics are this:

- While the image you have is "unzoomed", the scrollviewer has full control and the ViewportControl does nothing.

- When you start to pinch, lock the scrollviewer by disabling the verticalscrollbar AND setting the viewport.height = scrollviewer.height. This neutralizes the scollviewer.

- You can do a temporary zoom using the Image ScaleTransform.

- On pinch finished, resize your actual image so that it takes up real space inside the ViewportControl. Now your viewportControl will let you pan all over the zoomed image with nice bounce-back.

- When you zoom back out again, re-enable the scrollviewer. (Set the height to the screen height and turn on the scrollbar.)

FYI, I completely forget why there is a canvas in there, but I feel like it is important. See below:

While the sample below does not do what you want to do, I based my code on the MediaViewer inside this sample and modified it: Basic Lens Sample

However it should be noted that it is for picture zoom.

Problem

I’ve got n playing map where I use the `ScrollViewer` to move around the map, and I wish to use the `ViewBox` together with `PinchManipulations` to zoom in and out of the map. So far I’ve done this by setting the `ScrollViewer`’s `Manipulation` mode to `control`, however this gives me an lag when I zoom. Is there a way to get the `ViewBox` and `ScrollViewer` to work better together and thereby avoid the lag? The code I’ve got so far is: ScrollViewer: ``` <ScrollViewer Grid.Column ="0" Width="768" Height="380" HorizontalScrollBarVisibility="Hidden"> <Viewbox Stretch="None"> <View:Map/> </Viewbox> </ScrollViewer> ``` PinchZoom: ``` <Grid x:Name="Map" Width="1271" Height="1381.5"> <Grid.RenderTransform> <ScaleTransform ScaleX="{Binding Path=deltaZoom}" ScaleY="{Binding Path=deltaZoom}"/> </Grid.RenderTransform> <i:Interaction.Triggers> <i:EventTrigger EventName="ManipulationStarted"> <cmd:EventToCommand Command="{Binding Path=ZoomStartedCommand}" PassEventArgsToCommand="True"/> </i:EventTrigger> <i:EventTrigger EventName="ManipulationDelta"> <cmd:EventToCommand Command="{Binding Path=ZoomDeltaCommand}" PassEventArgsToCommand="True"/> </i:EventTrigger> <i:EventTrigger EventName="ManipulationCompleted"> <cmd:EventToCommand Command="{Binding Path=ZoomCompletedCommand}" PassEventArgsToCommand="True"/> </i:EventTrigger> </i:Interaction.Triggers> </Grid> ``` The code where I use the pinch zoom: ``` public ICommand ZoomStartedCommand { get; set; } public ICommand ZoomDeltaCommand { get; set; } public ICommand ZoomCompletedCommand { get; set; } private double _deltaZoom; public double deltaZoom { get { return _deltaZoom; } set { _deltaZoom = value; RaisePropertyChanged("deltaZoom"); } } public double distance; public MainViewModel() { ZoomStartedCommand = new RelayCommand<ManipulationStartedEventArgs>(ZoomStart); ZoomDeltaCommand = new RelayCommand<ManipulationDeltaEventArgs>(ZoomDelta); ZoomCompletedCommand = new RelayCommand<ManipulationCompletedEventArgs>(ZoomCompleted); } public void ZoomStart(ManipulationStartedEventArgs e) { FrameworkElement Element = (FrameworkElement)e.OriginalSource; var myScrollViewer = FindParentOfType<ScrollViewer>(Element) as ScrollViewer; myScrollViewer.SetValue(ScrollViewer.ManipulationModeProperty, ManipulationMode.Control); } public void ZoomDelta(ManipulationDeltaEventArgs e) { if (e.PinchManipulation != null) { deltaZoom = deltaZoom * e.PinchManipulation.DeltaScale; } else { FrameworkElement Element = (FrameworkElement)e.OriginalSource; var myScrollViewer = FindParentOfType<ScrollViewer>(Element) as ScrollViewer; myScrollViewer.SetValue(ScrollViewer.ManipulationModeProperty, ManipulationMode.System); } } public void ZoomCompleted(ManipulationCompletedEventArgs e) { FrameworkElement Element = (FrameworkElement)e.OriginalSource; var myScrollViewer = FindParentOfType<ScrollViewer>(Element) as ScrollViewer; myScrollViewer.SetValue(ScrollViewer.ManipulationModeProperty, ManipulationMode.System); } ```

Original source