views:

397

answers:

3

My XAML code is like this:

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

How can I, in XAML or in C#, eliminate the search box?

+1  A: 

You can replace a control template for it. For your reference: the default DocumentViewer's control template is here: http://msdn.microsoft.com/en-us/library/aa970452.aspx

The search toolbar's name is PART_FindToolBarHost, so you can also just assign its Visibility to Collapsed.

Vlad
A: 

Are you sure you need a DocumentViewer? You could use a FlowDocumentScrollViewer instead, or if you like pagination or multi-column display, you could use a FlowDocumentPageViewer.

Robert Lamb
I want a DocumentViewer because my goal is to produce print preview, and the XpsDocument is the thing that paginates visuals automatically. I could do this with a FDSV and some other custom code, but... I'd rather do the lazy thing.
Cheeso
you got me wondering how you do print preview for FlowDocuments...fwiw, from "Pro WPF in C# 2008" looks like you write the flow doc out as an XPS file then read it back in (as a fixed document) and finally display it in a DocumentViewer...wow!
Robert Lamb
@Robert Lamb - yes, that's how I do it. See http://stackoverflow.com/questions/2322064 .
Cheeso
@Cheeso - Nice, thank you! :)
Robert Lamb
+1  A: 

Vlad's answer led me to look at how to programmatically grab the ContentControl that holds the find toolbar. I didn't really want to write an entirely new template for the DocumentViewer; I wanted to change (hide) only one control. That reduced the problem to how to retrieve a control that is applied via a template?.
Here's what I figured out:

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