문제

간단한 질문. WPF에서 DocumentViewer의 텍스트 선택을 어떻게 비활성화합니까? 이것은 뷰어가 XPS 문서를 표시 한 다음 마우스를 통해 텍스트를 강조 할 수있는 기능입니다. 강조 표시된 텍스트도 복사 할 수 있지만 이미 비활성화했습니다. 하이라이트를 비활성화하는 방법을 모르겠습니다.

감사!

도움이 되었습니까?

해결책

우리는 DocumentViewer 컨트롤에 포함 된 스크롤 뷰어의 제어판을 무시함으로써 이것을 해결했습니다. "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>

그런 다음 d

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

그런 다음 "previewmouseleftbuttondown ="ScrollContentPresenter_previewmouseleftbuttondown "" "" ""CustomsCrollPresenter 스타일에 명시된 기능을 만듭니다.

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

다른 팁

또 다른 방법은 DockPanel을 추가하는 것입니다.

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

DocumentViewer를 "위"하고 예를 들어 페이지 로딩 이벤트에서 스크롤 뷰어 컨텐츠의 실제 너비와 높이로 너비와 높이를 설정합니다.

줌 옵션을 사용하고 수평 도구 모음이 표시되면 추가 로직을 추가해야 할 수도 있습니다.

isfocusable = false를 사용할 수 있습니다. 그러나 검색 상자도 비활성화됩니다 ...

xaml.cs part에서 다음 코드를 구현하십시오 (DocumentViewErinstance X : XAML에서 DocumentViewer의 이름).

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

당신은 사용할 수 있습니다 IsFocusable=false 또는 IsHitTestVisible = false 또는 선택 비활성화에 대한 미리보기 이벤트 (예 : 허용 된 답변)를 처리하지만 하이퍼 링크는 작동하지 않습니다! IsSelectionEnabled = false를 설정하면 선택이 비활성화되지만 하이퍼 링크도 작동합니다. (경고! isselectionenabled는 false를 설정 한 후에는 실제 값으로 변경 될 수 있으므로 값을 자주 확인해야합니다.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top