简体   繁体   English

禁用UWP中枢轴的左右滑动功能

[英]Disable swipe left/right feature of Pivot in UWP

I want to cover my pivot with ScrollViewer but it does not effect because pivot has it scrollviewer itself. 我想用ScrollViewer覆盖我的数据透视,但是它没有效果,因为数据透视有它自己的scrollviewer。 So I want to know the way to disable swipe left/right to change tab of Pivot. 所以我想知道禁用向左/向右滑动以更改“ Pivot”选项卡的方法。

Note: cannot use IsHitTestVisible="True" because it will disable all interaction of Pivot include tap on header to change tab 注意:不能使用IsHitTestVisible="True"因为它将禁用Pivot的所有交互,包括点击标题以更改选项卡

If you want to Disable Left/Right Swipe on Pivot but still want to navigate to Different Pivot Items when Header is tapped, You need to Disable HorizontalScrollMode on ScrollViewer on the Pivot when Pointer is PointerEntered or PointerMoved 如果要禁用枢轴上的向左/向右滑动,但仍想在点按标题时导航到其他枢轴项,则需要在Pointer为PointerEnteredPointerMoved时在枢轴上的ScrollViewer上禁用HorizontalScrollMode

To Do this first We need to Gain Access to ScrollViewer Inside Pivot . 首先要执行此操作,我们需要访问Pivot内部的ScrollViewer To do this, I used a Helper Method that I copied from one of the answers from SO. 为此,我使用了一个辅助方法,该方法是从SO的答案之一中复制的。

public static T FindChildByName<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 = FindChildByName<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;
}

Now in Pivot , Activate Event Pivot_Loaded and your Pivot_Loaded should show something like below. 现在在Pivot ,激活事件Pivot_Loaded ,您的Pivot_Loaded应该显示如下所示。

private void Pivot_Loaded(object sender, RoutedEventArgs e)
{
    Pivot pivot = sender as Pivot;
    int count = VisualTreeHelper.GetChildrenCount(pivot);
    ScrollViewer scrollViewer = FindChildByName<ScrollViewer>(pivot, "ScrollViewer");
    scrollViewer.PointerEntered += (s, a) => { ((ScrollViewer)s).HorizontalScrollMode = ScrollMode.Disabled; };
    scrollViewer.PointerMoved += (s, a) => { ((ScrollViewer)s).HorizontalScrollMode = ScrollMode.Disabled; };
    scrollViewer.PointerExited += (s, a) => { ((ScrollViewer)s).HorizontalScrollMode = ScrollMode.Enabled; };
    scrollViewer.PointerReleased += (s, a) => { ((ScrollViewer)s).HorizontalScrollMode = ScrollMode.Enabled; };
    scrollViewer.PointerCaptureLost += (s, a) => { ((ScrollViewer)s).HorizontalScrollMode = ScrollMode.Enabled; };
}

The whole idea of doing this is from this blog post. 这样做的整个想法来自这篇博客文章。

Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM