如何在按住并拖动添加到ListView的可滚动用户控件时禁用滚动?

时间:2018-03-23 10:11:54

标签: listview uwp uwp-xaml scrollviewer

我有一个包含ScrollViewer的可滚动用户控件,该用户控件被添加到listview项目中。当我尝试使用触摸输入保持listview项并拖动时,ListViewItem中的ScrollViewer拦截并开始滚动。所以我无法拖动和重新排序项目。

我现在使用的代码:

XAML:

<Page
    x:Class="App2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" VerticalAlignment="Center" HorizontalAlignment="Center">
        <ListView x:Name="listView" CanReorderItems="true" AllowDrop="true" CanDragItems="true">
        </ListView>
    </Grid>
</Page>

C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace App2
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            ScrollViewer content1 = new ScrollViewer() { MinZoomFactor = 0.1f, MaxZoomFactor = 3,
                ZoomMode = ZoomMode.Enabled ,
                VerticalScrollBarVisibility = ScrollBarVisibility.Auto
                ,HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
                HorizontalContentAlignment = HorizontalAlignment.Center, 
                Width=300, Height=200
            };
            content1.ManipulationMode = ManipulationModes.TranslateRailsX | ManipulationModes.TranslateY;
            StackPanel panel = new StackPanel() { Width = 600, Height = 1000, Background = new SolidColorBrush(global::Windows.UI.Color.FromArgb(255, 255, 0, 0))};

            content1.Content = panel;

            ScrollViewer content2 = new ScrollViewer() { MinZoomFactor = 0.1f, MaxZoomFactor = 3,
                ZoomMode = ZoomMode.Enabled ,
                VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
                HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                Width = 300,
                Height = 200
            };
            content2.ManipulationMode = ManipulationModes.TranslateRailsX | ManipulationModes.TranslateY;

            StackPanel panel1 = new StackPanel() { Width = 600, Height = 1000, Background = new SolidColorBrush(global::Windows.UI.Color.FromArgb(255, 0, 0, 255))};

            content2.Content = panel1;

            ScrollViewer content3 = new ScrollViewer()
            {
                MinZoomFactor = 0.1f,
                MaxZoomFactor = 3,
                ZoomMode = ZoomMode.Enabled,
                VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
                HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                Width = 300,
                Height = 200
            };

            content3.ManipulationMode = ManipulationModes.TranslateRailsX | ManipulationModes.TranslateY;
            StackPanel panel2 = new StackPanel() { Width = 600, Height = 1000, Background = new SolidColorBrush(global::Windows.UI.Color.FromArgb(255, 0, 255, 0)) };

            content3.Content = panel2;
            content1.AllowDrop = true;
            content2.AllowDrop = true;
            content3.AllowDrop = true;


            listView.Items.Add(content1);
            listView.Items.Add(content2);
            listView.Items.Add(content3);
        }
    }
}

0 个答案:

没有答案