在DocumentViewer中禁用文本选择 [英] Disabling text selection in DocumentViewer

查看:114
本文介绍了在DocumentViewer中禁用文本选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题.如何在WPF中禁用DocumentViewer的文本选择?这是查看器显示XPS文档,然后可以通过鼠标突出显示文本的功能.突出显示的文本也可以复制,但是我已经禁用了它.我只是不知道如何禁用突出显示.

Simple question. How do you disable the text selection of DocumentViewer in WPF? This is the feature where an XPS document is displayed by the viewer and then text can be highlighted via mouse. The highlighted text can also be copied but I have already disabled this. I just don't know how to disable the highlighting.

谢谢!

推荐答案

我们已通过重写DocumentViewer控件中嵌入的ScrollViewer的ControlTemplate来解决此问题.在"Window.Resources"中插入以下样式:

We have solved this by overriding the ControlTemplate of the ScrollViewer embedded in the DocumentViewer control. Insert the Style below in "Window.Resources":

<Style TargetType="{x:Type ScrollViewer}"  x:Key="CustomScrollPresenter">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                <Grid Background="{TemplateBinding Panel.Background}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Rectangle Grid.Column="1" Grid.Row="1" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                    <ScrollContentPresenter 
                        PreviewMouseLeftButtonDown="ScrollContentPresenter_PreviewMouseLeftButtonDown"
                        Grid.Column="0" 
                        Grid.Row="0" 
                        Margin="{TemplateBinding Control.Padding}" 
                        Content="{TemplateBinding ContentControl.Content}" 
                        ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" 
                        CanContentScroll="{TemplateBinding ScrollViewer.CanContentScroll}" />
                    <ScrollBar 
                        x:Name="PART_VerticalScrollBar"
                        Grid.Column="1" 
                               Grid.Row="0" 
                               Minimum="0" 
                               Maximum="{TemplateBinding ScrollViewer.ScrollableHeight}" 
                               ViewportSize="{TemplateBinding ScrollViewer.ViewportHeight}" 
                               Value="{Binding Path=VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource Mode=TemplatedParent}}" 
                               Visibility="{TemplateBinding ScrollViewer.ComputedVerticalScrollBarVisibility}" 
                               Cursor="Arrow" AutomationProperties.AutomationId="VerticalScrollBar" />
                    <ScrollBar 
                        x:Name="PART_HorizontalScrollBar"
                        Orientation="Horizontal" Grid.Column="0" Grid.Row="1" Minimum="0" 
                               Maximum="{TemplateBinding ScrollViewer.ScrollableWidth}" ViewportSize="{TemplateBinding ScrollViewer.ViewportWidth}" Value="{Binding Path=HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource Mode=TemplatedParent}}" Visibility="{TemplateBinding ScrollViewer.ComputedHorizontalScrollBarVisibility}" Cursor="Arrow" AutomationProperties.AutomationId="HorizontalScrollBar" />

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后在DocumentViewer的ControlTemplate中使用ScrollViewer的样式覆盖它:

Then override the Style of ScrollViewer with it in the ControlTemplate for DocumentViewer:

   <Style
      x:Key="MyDVStyleExtend"
      BasedOn="{StaticResource {x:Type DocumentViewer}}"
      TargetType="{x:Type DocumentViewer}">

      <Setter Property="Template">                
       <Setter.Value>

          <ControlTemplate TargetType="DocumentViewer">
                        <Border BorderThickness="2,2,2,2"
                    BorderBrush="SlateBlue" Focusable="False">
              <Grid Background="{StaticResource GridBackground}" 
                KeyboardNavigation.TabNavigation="Local">
                <Grid.ColumnDefinitions>                  
                  <ColumnDefinition Width ="*"/>                                    
                </Grid.ColumnDefinitions>                

                <ScrollViewer Style="{StaticResource CustomScrollPresenter}"  Grid.Column ="0" 
                  CanContentScroll="True"
                  HorizontalScrollBarVisibility="Auto"
                  x:Name="PART_ContentHost"
                  IsTabStop="True"/>

              </Grid>
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>

    </Style>

然后为以CustomScrollPresenter样式声明的"PreviewMouseLeftButtonDown ="ScrollContentPresenter_PreviewMouseLeftButtonDown"属性创建一个函数.

Then create a function for the "PreviewMouseLeftButtonDown="ScrollContentPresenter_PreviewMouseLeftButtonDown"" attribute stated in the CustomScrollPresenter style.

  private void ScrollContentPresenter_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        e.Handled = true;
    }

这篇关于在DocumentViewer中禁用文本选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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