UWP 禁止Pivot swip 手势

以前想要禁止内置的手势动作,看了一下网上是设置 IsLocked="True".

但是拿到UWP上来,靠,设置了之后header只显示当前的那个header。这样的设计真是丑爆了。。没办法,只能自己研究。看了看Pivot的模板,找到个方法。

不多说。直接上代码。

        <Pivot >
            <PivotItem Header="Header1">
                <Grid ManipulationMode="TranslateX" PointerPressed="Grid_PointerPressed" PointerReleased="Grid_PointerReleased">
                    
                </Grid>
            </PivotItem>
            <PivotItem Header="Header2">

            </PivotItem>
            <PivotItem Header="Header3">

            </PivotItem>
        </Pivot>

比如说我想在第一个item里面做一些拖拽的动作,因为有内置手势,没法直接使用。
为Grid增加上面的事件和属性设置,注意

ManipulationMode="TranslateX" 是必然设置的,根据你自己的需求。

再看一下后台的代码

        Pivot pivot = null;
        private void Grid_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            if (pivot == null)
            {
                var parent = (sender as Grid).Parent as FrameworkElement;
                while (parent != null)
                {
                    pivot = parent as Pivot;
                    if (pivot != null)
                    {
                        ScrollViewer.SetHorizontalScrollMode(pivot, ScrollMode.Disabled);
                        break;
                    }
                    parent = parent.Parent as FrameworkElement;
                }

            }
            else
            {
                ScrollViewer.SetHorizontalScrollMode(pivot, ScrollMode.Disabled);
            }
        }

        private void Grid_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            if (pivot != null)
            {
                ScrollViewer.SetHorizontalScrollMode(pivot, ScrollMode.Enabled);
            }
        }

控制ScrollViewer的HorizontalScrollMode 属性。。我上面写成这样是因为,项目里面不是简单的grid,是一个自定义的页面。反正意思就是去拿到Pivot,进行设置就ok了。

有更好的办法的同学分享一下。

 

又查看了一下模板,修改了模板之后还是可以继续使用IsLocked 属性。注意蓝色部分,将它注释掉就可以了

<Style TargetType="PivotHeaderItem">
<Setter Property="FontSize" Value="{ThemeResource PivotHeaderItemFontSize}" />
<Setter Property="FontFamily" Value="{ThemeResource PivotHeaderItemFontFamily}" />
<Setter Property="FontWeight" Value="{ThemeResource PivotHeaderItemThemeFontWeight}" />
<Setter Property="CharacterSpacing" Value="{ThemeResource PivotHeaderItemCharacterSpacing}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseMediumBrush}" />
<Setter Property="Padding" Value="{ThemeResource PivotHeaderItemMargin}" />
<Setter Property="Height" Value="48" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="PivotHeaderItem">
      <Grid
          x:Name="Grid"
          Background="{TemplateBinding Background}">
        <Grid.Resources>
          <Style x:Key="BaseContentPresenterStyle" TargetType="ContentPresenter">
            <Setter Property="FontFamily" Value="XamlAutoFontFamily"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="FontSize" Value="15"/>
            <Setter Property="TextWrapping" Value="Wrap"/>
            <Setter Property="LineStackingStrategy" Value="MaxHeight"/>
            <Setter Property="TextLineBounds" Value="Full"/>
            <Setter Property="OpticalMarginAlignment" Value="TrimSideBearings"/>
          </Style>
          <Style x:Key="BodyContentPresenterStyle" TargetType="ContentPresenter" BasedOn="{StaticResource BaseContentPresenterStyle}">
            <Setter Property="FontFamily" Value="{ThemeResource PivotHeaderItemFontFamily}" />
            <Setter Property="FontWeight" Value="{ThemeResource PivotHeaderItemThemeFontWeight}"/>
            <Setter Property="FontSize" Value="{ThemeResource PivotHeaderItemFontSize}"/>
          </Style>
        </Grid.Resources>
        <VisualStateManager.VisualStateGroups>
          <VisualStateGroup x:Name="SelectionStates">
            <VisualStateGroup.Transitions>
              <VisualTransition From="Unselected" To="UnselectedLocked" GeneratedDuration="0:0:0.33" />
              <VisualTransition From="UnselectedLocked" To="Unselected" GeneratedDuration="0:0:0.33" />
            </VisualStateGroup.Transitions>
            <VisualState x:Name="Disabled">
              <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                               Storyboard.TargetProperty="Foreground" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
                </ObjectAnimationUsingKeyFrames>
              </Storyboard>
            </VisualState>
            <VisualState x:Name="Unselected" />
            <VisualState x:Name="UnselectedLocked">
              <Storyboard>
                <DoubleAnimation Storyboard.TargetName="ContentPresenterTranslateTransform"
                                 Storyboard.TargetProperty="X"
                                 Duration="0" To="{ThemeResource PivotHeaderItemLockedTranslation}" />
                <DoubleAnimation Storyboard.TargetName="ContentPresenter"
                                 Storyboard.TargetProperty="(UIElement.Opacity)"
                                 Duration="0" To="0" />
              </Storyboard>
            </VisualState>
            <VisualState x:Name="Selected">
              <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                               Storyboard.TargetProperty="Foreground" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                               Storyboard.TargetProperty="Background" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                </ObjectAnimationUsingKeyFrames>
              </Storyboard>
            </VisualState>
            <VisualState x:Name="UnselectedPointerOver">
              <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                               Storyboard.TargetProperty="Foreground" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                               Storyboard.TargetProperty="Background" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                </ObjectAnimationUsingKeyFrames>
              </Storyboard>
            </VisualState>
            <VisualState x:Name="SelectedPointerOver">
              <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                Storyboard.TargetProperty="Foreground" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                               Storyboard.TargetProperty="Background" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                </ObjectAnimationUsingKeyFrames>
              </Storyboard>
            </VisualState>
            <VisualState x:Name="UnselectedPressed">
              <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                               Storyboard.TargetProperty="Foreground" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                               Storyboard.TargetProperty="Background" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                </ObjectAnimationUsingKeyFrames>
              </Storyboard>
            </VisualState>
            <VisualState x:Name="SelectedPressed">
              <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                               Storyboard.TargetProperty="Foreground" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                               Storyboard.TargetProperty="Background" >
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                </ObjectAnimationUsingKeyFrames>
              </Storyboard>
            </VisualState>
          </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <ContentPresenter
            x:Name="ContentPresenter"
            Content="{TemplateBinding Content}"
            ContentTemplate="{TemplateBinding ContentTemplate}"
            Margin="{TemplateBinding Padding}"
            FontSize="{TemplateBinding FontSize}"
            FontFamily="{TemplateBinding FontFamily}"
            FontWeight="{TemplateBinding FontWeight}"
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
          <ContentPresenter.RenderTransform>
            <TranslateTransform x:Name="ContentPresenterTranslateTransform" />
          </ContentPresenter.RenderTransform>
        </ContentPresenter>
      </Grid>
    </ControlTemplate>
  </Setter.Value>
</Setter>
</Style>

 

posted @ 2016-03-23 10:25  法的空间  阅读(557)  评论(0编辑  收藏  举报