views:

1014

answers:

4

Hello!

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.

Thanks!

A: 

you may use IsFocusable=false. But search box will be disabled too...

+1  A: 

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>

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>

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;
    }
A: 

I need to disable the whole find control from the toolbar ... any idea??

Rohit Kandhal
Try this http://sdx2000.blogspot.com/2010/03/removing-documentviewer-search-text-box.html
SDX2000
A: 
    <!-- Document Viewer Template-->
    <Style TargetType="{x:Type DocumentViewer}"
           x:Key="CustomDocumentViewer">

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="DocumentViewer"
                                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                 xmlns:swd="clr-namespace:System.Windows.Documents;assembly=PresentationUI"
                                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
                    <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
                            BorderBrush="{TemplateBinding Border.BorderBrush}"
                            Focusable="False">
                        <Grid Background="{TemplateBinding Panel.Background}"
                              KeyboardNavigation.TabNavigation="Local">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <ContentControl TabIndex="0"
                                            Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly=swd:PresentationUIStyleResources, ResourceId=PUIDocumentViewerToolBarStyleKey}}"
                                            Focusable="{TemplateBinding UIElement.Focusable}"
                                            Grid.Column="0"
                                            Grid.Row="0" />
                            <ScrollViewer CanContentScroll="True"
                                          HorizontalScrollBarVisibility="Auto"
                                          TabIndex="1"
                                          IsTabStop="True"
                                          Name="PART_ContentHost"
                                          Focusable="{TemplateBinding UIElement.Focusable}"
                                          Grid.Column="0"
                                          Grid.Row="1" />
                            <DockPanel Grid.Row="1">
                                <FrameworkElement Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"
                                                  DockPanel.Dock="Right" />
                                <Rectangle Height="10"
                                           VerticalAlignment="Top"
                                           Visibility="Visible">
                                    <Rectangle.Fill>
                                        <LinearGradientBrush StartPoint="0,0"
                                                             EndPoint="0,1">
                                            <LinearGradientBrush.GradientStops>
                                                <GradientStop Color="#66000000"
                                                              Offset="0" />
                                                <GradientStop Color="#00FFFFFF"
                                                              Offset="1" />
                                            </LinearGradientBrush.GradientStops>
                                        </LinearGradientBrush>
                                    </Rectangle.Fill>
                                </Rectangle>
                            </DockPanel>
                            <!-- Mantis # 20873 Hide search textbox -->
                            <!--<ContentControl TabIndex="2"
                                            Name="PART_FindToolBarHost"
                                            Focusable="{TemplateBinding UIElement.Focusable}"
                                            Grid.Column="0"
                                            Grid.Row="2" />-->
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Over ride the default template of document viewer.

Rohit Kandhal