1

This question has a solution: How do you hide a WPF DocumentViewer's menu bars?

However, it only allows hiding the toolbar through XAML. I need to get this done programmatically.

This answer: WPF: How can I remove the searchbox in a DocumentViewer? hides the search bar programmatically.

How do I hide the main toolbar via non-xaml code?

Community
  • 1
  • 1
SharpAffair
  • 5,558
  • 13
  • 78
  • 158
  • I think it's the simplest and best workaround: http://stackoverflow.com/questions/1649325/how-do-you-hide-a-wpf-documentviewers-menu-bars – Ali Asgari Aug 02 '14 at 11:44

1 Answers1

4

There is nothing in a DocumentViewer which ensures that the toolbar is even there, that being the case a programmatic manipulation of the control at run-time to remove a toolbar which may or may not exist might not be such a good idea. Of course you can do some null-checking and exception handling but that is not very clean either.

For the default aero template the following code will knock out the toolbar:

var contentHost = viewer.Template.FindName("PART_ContentHost", viewer) as ScrollViewer;
var grid = contentHost.Parent as Grid;
grid.Children.RemoveAt(0);

I remove the toolbar indirectly as it is not a PART, this is the reason why it may not even exist in some themes.

Ideally you should override the template completely.

H.B.
  • 166,899
  • 29
  • 327
  • 400