有没有办法在 WPF ScrollViewer 中启用触摸滚动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2829697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:45:39  来源:igfitidea点击:

Is there a way to touch-enable scrolling in a WPF ScrollViewer?

wpftouch

提问by Brian Sullivan

I'm trying to create a form in a WPF application that will allow the user to use iPhone-like gestures to scroll through the available fields. So, I've put all my form controls inside a StackPanel inside a ScrollViewer, and the scrollbar shows up as expected when there are too many elements to be shown on the screen.

我正在尝试在 WPF 应用程序中创建一个表单,该表单将允许用户使用类似 iPhone 的手势来滚动可用字段。因此,我已将所有表单控件放在 ScrollViewer 内的 StackPanel 中,并且当屏幕上显示的元素过多时,滚动条会按预期显示。

However, when I try to test this on my touch-enabled device, a panning gesture (placing a finger down on the surface and dragging it upward) does not move the viewable area down as I would expect.

但是,当我尝试在支持触摸的设备上对此进行测试时,平移手势(将手指向下放在表面上并向上拖动)不会像我预期的那样向下移动可视区域。

When I simply put a number of elements inside a ListView, the touch gestures work just fine. Is there any way to enable the same kind of behavior in a ScrollViewer?

当我简单地将一些元素放入 ListView 时,触摸手势工作得很好。有没有办法在 ScrollViewer 中启用相同类型的行为?

My window is structured like this:

我的窗口结构如下:

 <Window x:Class="TestTouchScrolling.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>        
    <ScrollViewer Name="viewer" VerticalScrollBarVisibility="Auto">
        <StackPanel Name="panel">
            <StackPanel Orientation="Horizontal">
                <Label>Label 1:</Label>
                <TextBox Name="TextBox1"></TextBox>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label>Label 2:</Label>
                <TextBox Name="TextBox2"></TextBox>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label>Label 3:</Label>
                <TextBox Name="TextBox3"></TextBox>
            </StackPanel>

            <!-- Lots more like these -->

        </StackPanel>
    </ScrollViewer>
</Grid>

回答by Valerie

You should use the attached properties:

您应该使用附加属性:

  • ScrollViewer.PanningMode
  • ScrollViewer.PanningDeceleration
  • ScrollViewer.PanningRatio
  • ScrollViewer.PanningMode
  • ScrollViewer.PanningDeceleration
  • ScrollViewer.PanningRatio

The PanningMode defaults to None in the ScrollViewer default style, but setting it to another value will enable touch scrolling. I'm currently investigating using this feature in my app and am looking for a good deceleration and ratio value... I'll probably just have to test them out to find something that works well.

PanningMode 在 ScrollViewer 默认样式中默认为 None,但将其设置为另一个值将启用触摸滚动。我目前正在调查在我的应用程序中使用此功能,并正在寻找一个好的减速和比率值......我可能只需要测试它们以找到一些运行良好的东西。

回答by Jobi Joy

If you are using .NET 4.0, there is a cool thing recently released by Microsoft team!! They ported all those nice Surface controls to Win7. SurfaceScrollViewer is really cool like iphone one. Install this toolkit and start a SurfaceWin7Touch project from VS2010 project template http://www.microsoft.com/en-us/download/details.aspx?id=26716

如果你使用的是 .NET 4.0,微软团队最近发布了一个很酷的东西!!他们将所有那些不错的 Surface 控件移植到 Win7。SurfaceScrollViewer 真的很酷,就像 iphone 一样。安装此工具包并从 VS2010 项目模板启动 SurfaceWin7Touch 项目 http://www.microsoft.com/en-us/download/details.aspx?id=26716