如何禁用DocumentViewer控件中的「打印」按钮?
禁用DocumentViewer的打印按钮
有两种常用方法可以禁用DocumentViewer控件中的打印按钮:
方法一:修改控件模板
提取DocumentViewer的默认控件模板,找到打印按钮(默认名称为PrintButton),直接设置其IsEnabled="False"。示例代码如下:
<DocumentViewer x:Name="viewerDocument" Document="{Binding GeneratedDocument}"> <DocumentViewer.Template> <ControlTemplate TargetType="{x:Type DocumentViewer}"> <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Focusable="False"> <Grid Background="{TemplateBinding Background}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <ToolBar ToolBarTray.IsLocked="True" Focusable="False"> <Button Command="ApplicationCommands.Print" x:Name="PrintButton" IsEnabled="False" Content="打印"/> <Button Command="ApplicationCommands.Copy" x:Name="CopyButton" Content="复制"/> <Separator/> <Button Command="NavigationCommands.IncreaseZoom" x:Name="IncreaseZoomButton" Content="放大"/> <Button Command="NavigationCommands.DecreaseZoom" x:Name="DecreaseZoomButton" Content="缩小"/> <Separator/> <Button Command="NavigationCommands.GoToPage" x:Name="GoToPageButton" Content="转到页面"/> <Button Command="NavigationCommands.Find" x:Name="FindButton" Content="查找"/> <Separator/> <Button Command="NavigationCommands.PreviousPage" x:Name="PreviousPageButton" Content="上一页"/> <Button Command="NavigationCommands.NextPage" x:Name="NextPageButton" Content="下一页"/> <Separator/> <Button Command="NavigationCommands.FirstPage" x:Name="FirstPageButton" Content="首页"/> <Button Command="NavigationCommands.LastPage" x:Name="LastPageButton" Content="尾页"/> </ToolBar> <ScrollViewer Grid.Row="1" CanContentScroll="True" HorizontalScrollBarVisibility="Auto" x:Name="PART_ContentHost" VerticalScrollBarVisibility="Auto"/> <StatusBar Grid.Row="2" Focusable="False"> <TextBlock x:Name="PageNumber" Text="{Binding PageNumber, RelativeSource={RelativeSource TemplatedParent}}"/> <TextBlock Text=" / "/> <TextBlock x:Name="PageCount" Text="{Binding PageCount, RelativeSource={RelativeSource TemplatedParent}}"/> </StatusBar> </Grid> </Border> </ControlTemplate> </DocumentViewer.Template> </DocumentViewer>
方法二:通过后台代码查找并禁用按钮
在DocumentViewer的Loaded事件中,遍历控件的可视化树,找到打印按钮并设置其IsEnabled为false。示例C#代码如下:
private void viewerDocument_Loaded(object sender, RoutedEventArgs e) { var printButton = FindVisualChild<Button>(viewerDocument, "PrintButton"); if (printButton != null) { printButton.IsEnabled = false; } } private T FindVisualChild<T>(DependencyObject parent, string name) where T : FrameworkElement { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { var child = VisualTreeHelper.GetChild(parent, i); if (child is T element && element.Name == name) { return element; } else { var result = FindVisualChild<T>(child, name); if (result != null) return result; } } return null; }
记得在XAML中绑定Loaded事件:
<DocumentViewer x:Name="viewerDocument" Document="{Binding GeneratedDocument}" Loaded="viewerDocument_Loaded"/>
内容的提问来源于stack exchange,提问作者Fabrizio Ciavatta
相关产品推荐
相关产品推荐

