2

I would like to Collapse the Copy button in WPF Documentviewer control toolbar. I added a trigger in style to set visibility to Collapsed.But it didn't work.Any thoughts why?

 <DocumentViewer Grid.Row="1" Margin="0,0,40,0" Name="documentViewer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
            <DocumentViewer.Resources>
                <Style TargetType="ContentControl">
                    <Style.Triggers>
                        <Trigger Property="Name" Value="PART_FindToolBarHost">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="Name" Value="CopyButton">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DocumentViewer.Resources>
        </DocumentViewer>
RaviG
  • 31
  • 4

2 Answers2

1

You can use this helper method to find CopyButton and hide it after DocumentViewer was loaded.

private void DocumentViewer_Loaded(object sender, RoutedEventArgs e)
{
    var button = UIHelper.FindChild<Button>(documentViewer, "CopyButton");
    button.Visibility = Visibility.Collapsed;
}

No Copy Button

Mitya
  • 632
  • 5
  • 18
  • I was surprised why trigger did not work. For now I go for code behind approach of finding the CopyButtin through visualtree and collapse it. Thanks Mitya. – RaviG Mar 12 '19 at 10:59
0

As far as I am aware there is no way to collapse the copy button without changing the style of the entire control... So here is a style suitable for you (warning a lot of code incomming)

(source = https://learn.microsoft.com/de-de/dotnet/framework/wpf/controls/documentviewer-styles-and-templates):

       <DocumentViewer Grid.Row="1" Margin="0,0,40,0" Name="documentViewer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <DocumentViewer.Style>
                <Style TargetType="{x:Type DocumentViewer}">
                    <Setter Property="Foreground"
      Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
                    <Setter Property="Background"
      Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                    <Setter Property="FocusVisualStyle"
      Value="{x:Null}" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DocumentViewer}">
                                <Border BorderThickness="{TemplateBinding BorderThickness}"
            BorderBrush="{TemplateBinding BorderBrush}"
            Focusable="False">
                                    <Grid KeyboardNavigation.TabNavigation="Local">

                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>
                                        <ToolBar ToolBarTray.IsLocked="True"
                 KeyboardNavigation.TabNavigation="Continue">
                                            <Button Command="ApplicationCommands.Print"
                  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                  Content="Print" />

                                            <Separator />
                                            <Button Command="NavigationCommands.IncreaseZoom"
                  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                  Content="Zoom In" />
                                            <Button Command="NavigationCommands.DecreaseZoom"
                  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                  Content="Zoom Out" />
                                            <Separator />
                                            <Button Command="NavigationCommands.Zoom"
                  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                  CommandParameter="100.0"
                  Content="Actual Size" />
                                            <Button Command="DocumentViewer.FitToWidthCommand"
                  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                  Content="Fit to Width" />
                                            <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand"
                  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                  CommandParameter="1"
                  Content="Whole Page" />
                                            <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand"
                  CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                  CommandParameter="2"
                  Content="Two Pages" />
                                        </ToolBar>

                                        <ScrollViewer Grid.Row="1"
                      CanContentScroll="true"
                      HorizontalScrollBarVisibility="Auto"
                      x:Name="PART_ContentHost"
                      IsTabStop="true">
                                        </ScrollViewer>

                                        <ContentControl Grid.Row="2"
                        x:Name="PART_FindToolBarHost"/>
                                    </Grid>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DocumentViewer.Style>
        </DocumentViewer>

Result:

enter image description here

Of cause you would have to modify it to fit your needs.

If you dont feel like working on a style based solution check out this link: WPF: How can I remove the searchbox in a DocumentViewer?

Denis Schaf
  • 2,478
  • 1
  • 8
  • 17
  • Hi Denis Thank you for your answer.I was aware of modifying the control template. But we loose the icons that we get with default toolbar. So I am concern for it as display of Icons look decent. Well we can add icons in each Button content of your solution but we don't get similar Icons and I am UX expert to do exact similar design. – RaviG Mar 11 '19 at 11:00
  • i understand and agree with you! A person below seems to know more check hius answer! Also for all kind of icon needs i usually use this website for private its free and for commercial its worth its money if you ask me https://icons8.de/ – Denis Schaf Mar 11 '19 at 11:54