禁用DocumentViewer中的文本选择功能。

7

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

谢谢!

4个回答

2
您可以使用IsFocusable=false。但搜索框也将被禁用...

1
这实际上解决了问题。对我来说,它也没有禁用搜索框,所以这是一个相当好的解决方案。请注意,属性名为Focusable而不是IsFocusable - dotNET

2
我们通过覆盖嵌入在DocumentViewer控件中的ScrollViewer的ControlTemplate来解决了这个问题。请将下面的样式插入到“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的样式:

   <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""属性创建一个函数。

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

这似乎确实禁用了文本选择,但它不允许我使用滚动。 - juFo

1
另一种方法是添加例如DockPanel:
<DockPanel Name="pnlTouchTaker" 
               VerticalAlignment="Bottom" HorizontalAlignment="Left"
               Background="Transparent">
    </DockPanel>

在页面加载事件中,将其宽度和高度设置为滚动视图器内容的实际宽度和高度,以覆盖文档查看器,并保留HTML标签。如果使用缩放选项并且水平工具栏变为可见,则可能需要添加额外的逻辑。

0

在 xaml.cs 部分实现以下代码(在你的 xaml 中,DocumentViewerInstance x:Name 是你的 DocumentViewer 名称)。

DocumentViewerInstance.GetType().GetProperty("IsSelectionEnabled", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(DocumentViewerInstance, false, null);

您可以使用IsFocusable=falseIsHitTestVisible=false或处理任何预览事件(例如在接受的答案中)来禁用选择,但超链接将无法工作!如果设置IsSelectionEnabled=false,则选择将被禁用,但超链接也将正常工作。(警告!在设置为false后,IsSelectionEnabled可能会更改为true值,因此您应经常检查该值。)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接