2010-02-24 28 views
9

mã XAML của tôi là như thế này:WPF: Làm thế nào tôi có thể xóa hộp tìm kiếm trong một DocumentViewer?

<Window 
    xmlns     ='http://schemas.microsoft.com/netfx/2007/xaml/presentation' 
    xmlns:x    ='http://schemas.microsoft.com/winfx/2006/xaml' 
    Title     ='Print Preview - More stuff here' 
    Height    ='200' 
    Width     ='300' 
    WindowStartupLocation ='CenterOwner'> 
    <DocumentViewer Name='dv1' ... /> 
</Window> 

Làm thế nào tôi có thể, trong XAML hoặc trong C#, loại bỏ hộp tìm kiếm?

Trả lời

13

Vlad's answer dẫn tôi xem cách lập trình ContentControl có chứa thanh công cụ tìm kiếm. Tôi thực sự không muốn viết một mẫu hoàn toàn mới cho DocumentViewer; Tôi muốn thay đổi (giấu) chỉ một điều khiển. Điều đó làm giảm vấn đề để làm thế nào để lấy một điều khiển được áp dụng thông qua một mẫu?.
Đây là những gì tôi đã tìm ra:

Window window = ... ; 
    DocumentViewer dv1 = LogicalTreeHelper.FindLogicalNode(window, "dv1") as DocumentViewer; 
    ContentControl cc = dv1.Template.FindName("PART_FindToolBarHost", dv1) as ContentControl; 
    cc.Visibility = Visibility.Collapsed; 
+4

+1. Nhưng: để làm cho điều này làm việc mà không có "findLogicalNode" và trước khi cửa sổ được hiển thị, nó cần mã của [Quarkonium response] (http://stackoverflow.com/a/9270218/218873) đầu tiên: 'dv1.ApplyTemplate(); ' – JYL

4

Bạn có thể thay thế mẫu kiểm soát cho nó. Để bạn tham khảo: mặc định DocumentViewer 's kiểm soát mẫu là ở đây: Tên http://msdn.microsoft.com/en-us/library/aa970452.aspx

Các thanh công cụ tìm kiếm là PART_FindToolBarHost, vì vậy bạn cũng có thể chỉ gán nó Visibility-Collapsed.


Edit:
Như nhận xét từ @ Martin cho thấy, các mẫu kiểm soát trong MSDN (tham chiếu ở trên) là không hoàn toàn chính xác. Một cách tốt hơn để trích xuất một mẫu thực sự được sử dụng trong WPF theo mặc định sẽ sử dụng Blend (Edit Control Template trong trình đơn ngữ cảnh, nếu tôi không nhầm lẫn).

+1

Đây là 'đúng' giải pháp ngoại trừ có một lỗi mà bạn sẽ nhận được chế độ thiết kế (không chạy thời gian) ngoại lệ ' 'zoom' được không phải là một giá trị hợp lệ cho bất động sản 'Command'.' . Xem http://connect.microsoft.com/VisualStudio/feedback/details/566538/document-viewer-controltemplate để biết thêm thông tin. –

+0

@Martin: cảm ơn, tôi đã cập nhật câu trả lời. – Vlad

0

Bạn có chắc chắn cần DocumentViewer không? Thay vào đó, bạn có thể sử dụng FlowDocumentScrollViewer hoặc nếu bạn thích hiển thị trang hoặc đa cột, bạn có thể sử dụng FlowDocumentPageViewer.

+0

Tôi muốn một DocumentViewer vì mục tiêu của tôi là tạo bản xem trước bản in và XpsDocument là thứ tự động loại bỏ hình ảnh. Tôi có thể làm điều này với một FDSV và một số mã tùy chỉnh khác, nhưng ... Tôi muốn làm điều lười biếng hơn. – Cheeso

+0

bạn đã tự hỏi làm thế nào bạn làm xem trước bản in cho FlowDocuments ... fwiw, từ "Pro WPF trong C# 2008" có vẻ như bạn viết luồng tài liệu ra dưới dạng tệp XPS rồi đọc nó trở lại (dưới dạng tài liệu cố định) và cuối cùng hiển thị nó trong một DocumentViewer ... wow! – RAL

+0

vâng, đó là cách tôi làm. Xem http://stackoverflow.com/questions/2322064. – Cheeso

4

Như Vlad chỉ ra bạn có thể thay thế các mẫu kiểm soát. Thật không may, mẫu điều khiển có sẵn trên MSDN không phải là mẫu điều khiển thực được sử dụng bởi điều khiển DocumentViewer. Dưới đây là các mẫu đúng sửa đổi để ẩn thanh tìm kiếm bằng cách thiết lập Visibility="Collapsed" trên PART_FindToolBarHost:

<!-- DocumentViewer style with hidden search bar. --> 
<Style TargetType="{x:Type DocumentViewer}" xmlns:Documents="clr-namespace:System.Windows.Documents;assembly=PresentationUI"> 
    <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="ContextMenu" Value="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerContextMenu, TypeInTargetAssembly={x:Type Documents:PresentationUIStyleResources}}}"/> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type DocumentViewer}"> 
     <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Focusable="False"> 
      <Grid Background="{TemplateBinding Background}" KeyboardNavigation.TabNavigation="Local"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="*"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 
      <ContentControl Grid.Column="0" Focusable="{TemplateBinding Focusable}" Grid.Row="0" Style="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerToolBarStyleKey, TypeInTargetAssembly={x:Type Documents:PresentationUIStyleResources}}}" TabIndex="0"/> 
      <ScrollViewer x:Name="PART_ContentHost" CanContentScroll="true" Grid.Column="0" Focusable="{TemplateBinding Focusable}" HorizontalScrollBarVisibility="Auto" IsTabStop="true" Grid.Row="1" TabIndex="1"/> 
      <DockPanel Grid.Row="1"> 
       <FrameworkElement DockPanel.Dock="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/> 
       <Rectangle Height="10" Visibility="Visible" VerticalAlignment="top"> 
       <Rectangle.Fill> 
        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> 
        <LinearGradientBrush.GradientStops> 
         <GradientStopCollection> 
         <GradientStop Color="#66000000" Offset="0"/> 
         <GradientStop Color="Transparent" Offset="1"/> 
         </GradientStopCollection> 
        </LinearGradientBrush.GradientStops> 
        </LinearGradientBrush> 
       </Rectangle.Fill> 
       </Rectangle> 
      </DockPanel> 
      <ContentControl x:Name="PART_FindToolBarHost" Grid.Column="0" Focusable="{TemplateBinding Focusable}" Grid.Row="2" TabIndex="2" Visibility="Collapsed"/> 
      </Grid> 
     </Border> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 

Bạn cần phải thêm một tham chiếu đến PresentationUI.dll. Hội đồng này nằm trong thư mục %WINDIR%\Microsoft.NET\Framework\v4.0.30319\WPF.

2

Để có được câu trả lời Cheeso để làm việc trong các nhà xây dựng tôi đã có thêm:

dv1.ApplyTemplate(); 

khác cc đi ra null. Xem câu trả lời here

11

Bạn có thể làm điều gì đó tương tự như Cheeso's answer với kiểu ContentControl và trình kích hoạt để ẩn khi tên là PART_FindToolBarHost.

<DocumentViewer> 
    <DocumentViewer.Resources> 
    <Style TargetType="ContentControl"> 
     <Style.Triggers> 
     <Trigger Property="Name" Value="PART_FindToolBarHost"> 
      <Setter Property="Visibility" Value="Collapsed" /> 
     </Trigger> 
     </Style.Triggers> 
    </Style> 
    </DocumentViewer.Resources> 
</DocumentViewer> 
1
<DocumentViewer> 
    <DocumentViewer.Resources> 
     <!-- Toolbar -->   
     <Style TargetType="ToolBar"> 
      <Setter Property="Visibility" Value="Collapsed" /> 
     </Style> 
      <!-- Search --> 
     <Style TargetType="ContentControl"> 
      <Setter Property="Visibility" Value="Collapsed" /> 
     </Style> 
    </DocumentViewer.Resources> 
</DocumentViewer> 
Các vấn đề liên quan