Как свернуть CopyButton в панели инструментов управления WPF DocumentViewer? - PullRequest
2 голосов
/ 11 марта 2019

Я хочу свернуть кнопку «Копировать» на панели инструментов управления WPF Documentviewer.Я добавил триггер в стиле, чтобы установить видимость Collapsed. Но это не сработало. Есть мысли почему?

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

Ответы [ 2 ]

1 голос
/ 11 марта 2019

Вы можете использовать этот вспомогательный метод , чтобы найти CopyButton и скрыть его после загрузки DocumentViewer.

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

No Copy Button

0 голосов
/ 11 марта 2019

Насколько я знаю, нет способа свернуть кнопку копирования, не изменив стиль всего элемента управления ... Так что вот подходящий вам стиль (предупреждающий большое количество кода, поступающего)

(источник = https://docs.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>

Результат:

enter image description here

Конечно, вам придется изменить его, чтобы он соответствовал вашемуНужны.

Если вы не хотите работать над решением на основе стиля, перейдите по этой ссылке: WPF: Как я могу удалить окно поиска в DocumentViewer?

...