在 WPF Scrollviewer 中的 Textbox 控件上启用滑动滚动 [英] Enable swipe scrolling on Textbox control in WPF Scrollviewer

查看:70
本文介绍了在 WPF Scrollviewer 中的 Textbox 控件上启用滑动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在 WPF 中开发触摸应用程序.我们的 ScrollViewers 将属性 PanningMode 设置为 Both 以启用滑动滚动.这在空白区域甚至在 CheckBox 和 ComboBox 控件上滑动时都可以正常工作.

We are developing a touch application in WPF. Our ScrollViewers have the property PanningMode set to Both to enable swipe scrolling. This works fine when swiping in empty areas and even on CheckBox and ComboBox controls.

但是,在 TextBox 上滑动时(将手指短暂地按住文本框并向上或向下移动),ScrollViewer 不会滚动.有没有办法在所有控件上启用滑动滚动并只将它们集中在点击上?

However when swiping on a TextBox (hold the finger briefly on the textbox and move it up or down), the ScrollViewer doesn't scroll. Is there a way to enable swipe-scrolling on all controls and only focus them on tap?

可以使用以下代码重现此行为:

This behaviour can be reproduced with the following code:

<Window x:Class="WpfSandbox.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">

    <ScrollViewer PanningMode="Both">
        <StackPanel>
            <TextBox Margin="5"></TextBox>
            <TextBox Margin="5"></TextBox>
            <TextBox Margin="5"></TextBox>
            <TextBox Margin="5"></TextBox>
            <TextBox Margin="5"></TextBox>
            <TextBox Margin="5"></TextBox>
            <TextBox Margin="5"></TextBox>
            <TextBox Margin="5"></TextBox>
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
        </StackPanel>
    </ScrollViewer>
</Window>

推荐答案

我已经设法解决了这个问题.TextBox 控件不滑动滚动的原因是因为它们的控件模板还包含一个 ScrollViewer,其 PanningMode 设置为 VerticalFirst.VerticalFirst 允许在水平滑动时选择 TextBox 文本(参见 MSDN PanningMode).

I've managed to solve this problem. The reason that TextBox controls don't swipe-scroll is because their control template also contains a ScrollViewer with a PanningMode set to VerticalFirst. VerticalFirst allows for selecting the TextBox text when swiping horizontally (see MSDN PanningMode).

仅当将 TextBox ScrollViewer.PanningMode 设置为 None 时,此控件上的滑动滚动才会保持(这将禁用选择).这是 CheckBox 和 ComboBox 控件的默认行为,因为它们的 ControlTemplate 中不包含 ScrollViewer.

Only when setting the TextBox ScrollViewer.PanningMode to None, swipe-scrolling on this control is maintained (this disables the selection). This is default behaviour for CheckBox and ComboBox controls because they do not contain a ScrollViewer in their ControlTemplate.

我修改了示例以显示此行为:

I've modified the example to show this behaviour:

<Window x:Class="WpfSandbox.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">

    <ScrollViewer PanningMode="Both">
        <StackPanel>
            <TextBox Margin="5" ScrollViewer.PanningMode="Both">Panningmode both</TextBox>
            <TextBox Margin="5" ScrollViewer.PanningMode="HorizontalFirst">Panningmode HorizontalFirst</TextBox>
            <TextBox Margin="5" ScrollViewer.PanningMode="HorizontalOnly">Panningmode HorizontalOnly</TextBox>
            <!-- Allows swipe scrolling -->
            <TextBox Margin="5" ScrollViewer.PanningMode="None">Panningmode None</TextBox>
            <TextBox Margin="5" ScrollViewer.PanningMode="VerticalFirst">Panningmode VerticalFirst</TextBox>
            <TextBox Margin="5" ScrollViewer.PanningMode="VerticalOnly">Panningmode VerticalOnly</TextBox>
            <TextBox Margin="5" ScrollViewer.PanningMode="Both" Height="60" xml:space="preserve" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" >Panning set to both&#x0a;test 2&#x0a;test 2&#x0a;test 2&#x0a;test 2&#x0a;test 2</TextBox>
            <!-- Allows swipe scrolling -->
            <TextBox Margin="5" ScrollViewer.PanningMode="None" Height="60" xml:space="preserve" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" >Panning set to none&#x0a;test 2&#x0a;test 2&#x0a;test 2&#x0a;test 2&#x0a;test 2</TextBox>
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <CheckBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
            <ComboBox Margin="5" />
        </StackPanel>
    </ScrollViewer>
</Window>

这篇关于在 WPF Scrollviewer 中的 Textbox 控件上启用滑动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆