0

I added a ScrollViewer to my UWP app for Windows 10.

I want to use it both for zooming and panning.

Here is my XAML:

<Page
    x:Class="LiquidTextUWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:lt="using:LiquidTextUWP.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
  <ScrollViewer VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden" 
                ZoomMode="Enabled" Background="DarkSeaGreen" Name="ScrollArea" 
                Width="10000" Height="10000">
    <Grid Background="LightGray" Name="Workspace">
      <!-- here I put all my other controls -->
    </Grid>
  </ScrollViewer>
</Page>

For Panning, I added a small event handler for the PointerMoved event, like this:

private double previousX = 0;

public void OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
    if (!(sender is ScrollViewer scrollViewer))
        return; 
    PointerPoint pp = e.GetCurrentPoint(null);
    double deltaX = pp.Position.X - previousX;
    previousDeltaX = pp.Position.X
    bool res = scrollViewer.ChangeView(scrollViewer.HorizontalOffset - deltaX, null, null);
    e.Handled = true;
}

It works nicely (i.e. it actually pans horizontally, and res = true) when scrollViewer.ZoomFactor > 1.

When instead scrollViewer.ZoomFactor < 1, it does nothing (and res = false).

EDIT: I also noticed that when scrollViewer.ZoomFactor < 1, the properties ScrollableWidth and ScrollableHeight goes to 0, while when scrollViewer.ZoomFactor > 1, the properties ScrollableWidth and ScrollableHeight are positive.

How can I enable the ScrollViewer to return true for ChangeView also when the zoom factor is less than 1?

Cristiano Ghersi
  • 1,944
  • 1
  • 20
  • 46
  • I tested your code when `ZoomFactor` >=2, it works. Could you share a mini sample that could reproduce this issue? – Nico Zhu Jan 08 '19 at 05:44
  • Hi Nico, sure, how can I share the sample with you? – Cristiano Ghersi Jan 09 '19 at 03:00
  • You could share code sample with one drive. – Nico Zhu Jan 09 '19 at 03:04
  • my apologies Nico, I just saw now that I switched the cases of working / not working! I update now my question. This code is not working for me when ZoomFactor < 1. Could you check again on your side? – Cristiano Ghersi Jan 09 '19 at 03:28
  • The `ZoomFactor` less than one, it means that a side of the content displayed fully and there is no sliding margin for scrolling. that is make sense. – Nico Zhu Jan 10 '19 at 07:59
  • Does this mean that I am messing up with Width and Height of the ScrollViewer vs its Content? I added the XAML of my Page, could you help me understanding how should I set Width&Height to get the content scrollable even when ZoomFactor < 1? – Cristiano Ghersi Jan 10 '19 at 22:38
  • I don't know what feature you want realize, when ZoomFactor<1, all the content will be displayed, why do you still want scroll the content? – Nico Zhu Jan 11 '19 at 07:25
  • This is my problem: not all the content is shown, therefore I would like to pan. I am assuming I am doing something wrong with the setup of Width & Height between the ScrollViewer and its Content (the Grid). Could you help me understanding where is the problem? – Cristiano Ghersi Jan 12 '19 at 01:32

2 Answers2

0

You have to set the ScrollViewer.ZoomMode to Enabled, I think.

Best regards

Luca Lindholm
  • 813
  • 1
  • 9
  • 23
0

For the sake of the people that will bump into this topic in the future, here's how I solved it:

I had to set a Width/Height of the ScrollArea way less than the Width/Height of the Workspace element.

My issue was to set the Width/Height of the ScrollArea to 10000. If I set instead to Auto (or leave it unexpressed) and instead I set the Width/Height of the Workspace to 10000, then I have the wanted effect.

Cristiano Ghersi
  • 1,944
  • 1
  • 20
  • 46