ScrollViewer:禁用Snap(弹性带效果)

时间:2013-04-22 09:42:17

标签: c# windows-8 windows-runtime

我的MainPage上有一个包含所有其他元素的ScrollViewer(当然除了AppBars)。滚动工作正常,但我想禁用捕捉行为(您将整个页面向左拖动,释放它,它将快速恢复)。查看图像(ScrollViewer背景:黑色,网格背景:白色)。那么如何禁用此行为?也称为过度滚动或反弹效应。

我刚刚发现 IsScrollInertiaEnabled ,但将此设置为false无效。

<ScrollViewer VerticalScrollMode="Disabled" 
                  VerticalScrollBarVisibility="Disabled" 
                  HorizontalScrollBarVisibility="Visible" 
                  ZoomMode="Disabled" 
                  HorizontalSnapPointsType="None" 
                  VerticalSnapPointsType="None" 
                  ZoomSnapPointsType="None">
    <VisualStateManager>...</VisualStatemanager>
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        ...
        <!-- My content grid -->

     </Grid>
</ScrollViewer>

enter image description here enter image description here

3 个答案:

答案 0 :(得分:1)

抱歉误解了你的意思..如果你可以发布一些额外的xaml .. scrollviewer容器xaml可能有帮助

尝试这样的事情

<Grid Style="{StaticResource LayoutRootStyle}">
    <Grid.RowDefinitions>
        <RowDefinition Height="140"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!-- Back button and page title -->
    ...
    <!-- My content grid -->
    <ScrollViewer Grid.Row="1"
      VerticalScrollMode="Disabled" 
        VerticalScrollBarVisibility="Disabled" 
        HorizontalScrollBarVisibility="Visible" 
        ZoomMode="Disabled" 
        HorizontalSnapPointsType="None" 
        VerticalSnapPointsType="None" 
        ZoomSnapPointsType="None">

        // put your content here directly or in a grid
    </ScrollViewer>
    <VisualStateManager>...</VisualStatemanager>
</Grid>

答案 1 :(得分:1)

要解决滚动查看器中对象的问题集高度,假设..如果你有一个堆栈面板,设置Height =“2000”或者......和DONE。 注意:不要在滚动查看器中放置任何高度。

答案 2 :(得分:0)

我有其他建议,我遇到了类似的问题,但我想禁用弹跳,只有1个元素在flipview中。

所以我这样做了:

创建了一个TemplatedControl:

CustomFlipView,它继承自FlipView:

public sealed class CustomFlipView : FlipView

接下来,我编辑了FlipViewStyle以获取FlipView的样式代码(是的,那个大的)。

复制粘贴到Generic.xaml中,生成

<Style TargetType="local:CustomFlipView">

所以这让我有机会为这样的xaml控件添加名称,默认情况下不会命名。

我已经为ItemsPresenter添加了一个名字:

<ScrollViewer x:Name="ScrollingHost" AutomationProperties.AccessibilityView="Raw" BringIntoViewOnFocusChange="{TemplateBinding ScrollViewer.BringIntoViewOnFocusChange}" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" HorizontalSnapPointsType="MandatorySingle" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsTabStop="False" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsHorizontalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsHorizontalScrollChainingEnabled}" IsVerticalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsVerticalScrollChainingEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" Padding="{TemplateBinding Padding}" TabNavigation="{TemplateBinding TabNavigation}" VerticalSnapPointsType="MandatorySingle" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" ZoomMode="Disabled">
    <ItemsPresenter x:Name="FlipViewItemsPresenter"/>
</ScrollViewer>

接下来在CustomFlipView的代码中我做了这个:

public sealed class CustomFlipView : FlipView //this line is indented.
{
    private ItemsPresenter itemsPresenter;

    public CustomFlipView()
    {
        this.DefaultStyleKey = typeof(CustomFlipView);
    }

    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        itemsPresenter = GetTemplateChild("FlipViewItemsPresenter") as ItemsPresenter;
        FixateItems();
    }

    protected override void OnItemsChanged(object e)
    {
        base.OnItemsChanged(e);
        FixateItems();
    }

    private void FixateItems()
    {
        if (itemsPresenter != null)
        {
            if (this.Items.Count < 2)
            {
                itemsPresenter.ManipulationMode = ManipulationModes.None;
            }
            else
            {
                itemsPresenter.ManipulationMode = ManipulationModes.System;
            }
        }
    }
}

希望这可以给你一个提示。