Search code examples
c#wpftoolbardropshadowdocumentviewer

DocumentViewer remove the ToolBar drop shadow


I have read lots of topics for removing toolbar or search bar in a DocumentViewer Control but i can't remove the drop shadow effect of the toolbar..

Do you have an idea ?

I have looped into children of the control but it's does not work.

enter image description here


Solution

  • Here is the visual tree (the selected Rectangle is the drop shadow your refer to):

    enter image description here

    The following code hides the Rectangle:

    class MyDocumentViewer : DocumentViewer
    {
        public void RemoveToolbarShadow()
        {
            var r = this.FindType<System.Windows.Controls.Border>()?
                .FindType<Grid>()?
                .FindType<DockPanel>()?
                .FindType<System.Windows.Shapes.Rectangle>();
    
            if (null != r) r.Visibility = Visibility.Hidden;
        }
    }
    

    Helper extension:

    static class DependencyObjectExtensions
    {
        internal static T FindType<T>(this DependencyObject reference) where T : DependencyObject
        {
            int n = VisualTreeHelper.GetChildrenCount(reference);
            for (int i = 0; i < n; i++)
            {
                var c = VisualTreeHelper.GetChild(reference, i) as T;
                if (null != c) return c;
            }
            return null;
        }
    }