WPF DocumentViewer enable scroll on touch screen for XPS document

核能气质少年 提交于 2019-12-24 12:12:45

问题


When showing an XPS document in the DocumentViewer control of a WPF application it does not allow you to scroll its content on a touch enabled tablet just my moving your fingers over the screen.

Instead it selects the text. The only way of scrolling on a touch enabled device is by using the vertical scrollbar.

Is there a way to enable touch scrolling by moving your fingers on the content itself instead of on the vertical scrollbar?

By overriding some styles I could prevent the text selection but it still does not allow me to scroll. ( https://stackoverflow.com/a/415155/187650 )


回答1:


I had the same problem and i made a solution and it works perfectly.

I made an own Xps Document class to set URI dynamically:

public class ManualXpsDocument : XpsDocument
{
    private const string _uriOfDoc= "...\\path.xps";

    public ManualXpsDocument(FileAccess fileAccess) : base(_uriOfDoc, fileAccess)
    {

    }
}

Here is the xaml part of window (or control):

  <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>

Here is The xaml.cs part of Window (or control):

    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 method solve the problem. This procedure do the scrolling of document viewer and disable the selection but the hyperlink function still available.



来源:https://stackoverflow.com/questions/46051049/wpf-documentviewer-enable-scroll-on-touch-screen-for-xps-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!