【问题标题】:WPF DocumentViewer enable scroll on touch screen for XPS documentWPF DocumentViewer 为 XPS 文档启用触摸屏滚动
【发布时间】:2018-02-13 12:25:58
【问题描述】:

当在 WPF 应用程序的 DocumentViewer 控件中显示 XPS 文档时,它不允许您在启用触摸的平板电脑上滚动其内容,只需在屏幕上移动手指即可。

而是选择文本。在支持触摸的设备上滚动的唯一方法是使用垂直滚动条。

有没有办法通过在内容本身而不是垂直滚动条上移动手指来启用触摸滚动?

通过覆盖某些样式,我可以阻止文本选择,但它仍然不允许我滚动。 (https://stackoverflow.com/a/415155/187650)

【问题讨论】:

    标签: wpf touch xps documentviewer xpsdocument


    【解决方案1】:

    我遇到了同样的问题,我提出了解决方案,效果很好。

    我制作了一个自己的 Xps Document 类来动态设置 URI:

    public class ManualXpsDocument : XpsDocument
    {
        private const string _uriOfDoc= "...\\path.xps";
    
        public ManualXpsDocument(FileAccess fileAccess) : base(_uriOfDoc, fileAccess)
        {
    
        }
    }
    

    这是窗口(或控件)的 xaml 部分:

      <Grid.Resources>
            <ObjectDataProvider x:Key="myDataSource" MethodName="GetFixedDocumentSequence"
                                ObjectType="{x:Type manual:ManualXpsDocument}">
                <ObjectDataProvider.ConstructorParameters>
                    <io:FileAccess>Read</io:FileAccess>
                </ObjectDataProvider.ConstructorParameters>
            </ObjectDataProvider>
        </Grid.Resources>
        <DocumentViewer  x:Name="Viewer" Document="{Binding Source={StaticResource myDataSource}}">
            <DocumentViewer.Resources>
                <Style TargetType="ToolBar">
                    <Setter Property="Visibility" Value="Collapsed" />
                </Style>
                <Style TargetType="{x:Type ContentControl}">
                    <Setter Property="Visibility" Value="Collapsed" />
                </Style>
            </DocumentViewer.Resources>
            <DocumentViewer.Style>
                <Style TargetType="{x:Type DocumentViewer}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DocumentViewer}">
                                <controls:CustomScrollViewer x:Name="PART_ContentHost">
                                    <controls:CustomScrollViewer.Style>
                                        <Style TargetType="{x:Type ScrollViewer}">
                                            <Setter Property="Focusable" Value="false" />
                                            <Setter Property="IsDeferredScrollingEnabled" Value="True" />
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                                        <Border BorderBrush="#00000000"
                                                            BorderThickness="0,2,0,0">
                                                            <Grid Background="{TemplateBinding Background}"
                                                                 SnapsToDevicePixels="true">
                                                                <Grid.ColumnDefinitions>
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="Auto" />
                                                                </Grid.ColumnDefinitions>
                                                                <Grid.RowDefinitions>
                                                                    <RowDefinition Height="*" />
                                                                    <RowDefinition Height="Auto" />
                                                                </Grid.RowDefinitions>
    
                                                                <ScrollContentPresenter Name="PART_ScrollContentPresenter"
                                                                                            ScrollViewer.IsDeferredScrollingEnabled="True"
                                                                                            KeyboardNavigation.DirectionalNavigation="Local"
                                                                                            CanContentScroll="True" 
                                                                                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
    
    
                                                                <controls:CustomScrollBar Name="PART_VerticalScrollBar"
                                                                                          Style="{DynamicResource ScrollBar}"
                                                                                          Grid.Column="1"
                                                                                          Value="{TemplateBinding VerticalOffset}"
                                                                                          Maximum="{TemplateBinding ScrollableHeight}"
                                                                                          ViewportSize="{TemplateBinding ViewportHeight}"
                                                                                          Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" />
                                                                <controls:CustomScrollBar Name="PART_HorizontalScrollBar"
                                                                                          Grid.Row="1"
                                                                                          Grid.ColumnSpan="2"
                                                                                          Style="{DynamicResource ScrollBar}"
                                                                                          Orientation="Horizontal"       
                                                                                          Value="{TemplateBinding HorizontalOffset}"
                                                                                          Maximum="{TemplateBinding ScrollableWidth}"
                                                                                          ViewportSize="{TemplateBinding ViewportWidth}"
                                                                                          Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" />
                                                            </Grid>
                                                        </Border>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </controls:CustomScrollViewer.Style>                                 
                                </controls:CustomScrollViewer>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DocumentViewer.Style>
        </DocumentViewer>
    

    这里是 Window(或控件)的 xaml.cs 部分:

        public ctor()
        {
            InitializeComponent();
    
            PreviewTouchMove += ScrollingHandler;
    
        }
    
        private void ScrollingHandler(object sender, TouchEventArgs e)
        {
            TouchPoint tp = e.GetTouchPoint(Viewer);
            if (tp.Action == TouchAction.Move)
            {
                //_scrollingInt is not necessary but Move procedure of Viewer has const value to scroll and the optimalization is recommended. 
                if (_lastYPosition > tp.Position.Y + _scrollingInt)
                {
                    Viewer.MoveDown();
                    _lastYPosition = tp.Position.Y;
                }  
                else if (_lastYPosition < tp.Position.Y - _scrollingInt)
                {
                    Viewer.MoveUp();
                    _lastYPosition = tp.Position.Y;
                }
            }
    
            // Viewer.IsHitTestVisible = false; this setting can disable the selection too,but if you use this, the hyperlinks won't work too.
    
            Viewer.GetType().GetProperty("IsSelectionEnabled", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(Viewer, false, null);
    
            //set false "IsSelectionEnabled" "hidden" property instead of Viewer.IsHitTestVisible = false, because the hyperlinks will work too. 
    
            e.Handled = true;
        }
    

    ScrollingHandler 方法解决了这个问题。此过程执行文档查看器的滚动并禁用选择,但超链接功能仍然可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      • 2010-11-29
      • 2016-06-12
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多