【问题标题】:Disabling text selection in DocumentViewer在 DocumentViewer 中禁用文本选择
【发布时间】:2010-09-26 23:03:48
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: wpf documentviewer textselection


    【解决方案1】:

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

    【讨论】:

    • 这实际上解决了问题。对我来说,它也不会禁用搜索框,所以这是一个很好的解决方案。请注意,该属性称为 Focusable 而不是 IsFocusable
    【解决方案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;
        }
    

    【讨论】:

    • 这似乎确实禁用了文本选择,但它不允许我使用滚动。
    【解决方案3】:

    另一种方法是添加例如停靠面板:

    <DockPanel Name="pnlTouchTaker" 
                   VerticalAlignment="Bottom" HorizontalAlignment="Left"
                   Background="Transparent">
        </DockPanel>
    

    位于文档查看器“上方”并将其宽度和高度设置为滚动查看器内容的实际宽度和高度,例如页面加载事件。

    如果使用缩放选项并且水平工具栏变得可见,您可能需要添加额外的逻辑。

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2012-10-26
        • 2013-06-27
        • 2012-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-05
        • 2010-12-11
        相关资源
        最近更新 更多