0

I am trying to find a solution to allow a Hub to pan with the mouse when its reaches the left or right boundary. I have implemented the code below which i have gleaned from various sources.

` private void theHubPointerMoved(object sender, PointerRoutedEventArgs e)
    { Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;

        if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
        {
            Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(null);
              if (ptrPt.Position.X < this.ActualWidth - 20)
                if (ptrPt.Position.X > 20)
                {
                    //Do the SCROLLING HERE
                    var xcord = Math.Round(ptrPt.Position.X, 2);
                    var ycord = Math.Round(ptrPt.Position.Y, 2);
                }
        }
           e.Handled = true;
    }`

So it is relativley easy to see when the mouse is at the screen edge. I thought it would be easy to simply use the MyHub.ScrollViewer.ScrollToHorizontalOffset(xcord); but the Hub Scrollviewer doesnt expose this ScrollToHorizontalOffset function.

Can anyone assist?

Thanks.

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
ckglobalroaming
  • 267
  • 3
  • 10

1 Answers1

0

Oh, it's exposed. If you can handle digging for it. Here's how:

http://xaml.codeplex.com/SourceControl/latest#Blog/201401-ScrollHub/MainPage.xaml.cs

In the example below, it is scrolling to a specific hub section. But you should be able to easily adapt it to your specific needs, I hope.

private void ScollHubToSection(Hub hub, HubSection section)
{
    var visual = section.TransformToVisual(this.MyHub);
    var point = visual.TransformPoint(new Point(0, 0));
    var viewer = Helpers.FindChild<ScrollViewer>(hub, "ScrollViewer");
    viewer.ChangeView(point.X, null, null);
}

Using this:

public class Helpers
{
    public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
    {
        if (parent == null) return null;
        T foundChild = null;
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            T childType = child as T;
            if (childType == null)
            {
                foundChild = FindChild<T>(child, childName);
                if (foundChild != null) break;
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    foundChild = (T)child;
                    break;
                }
            }
            else
            {
                foundChild = (T)child;
                break;
            }
        }
        return foundChild;
    }
}

Best of luck!

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233