DocumentToolbar always return null

I want to disable [Save As] button and [Print] button of pdfViewer, but when i try 

	DocumentToolbar toolbar = pdfViewer.Template.FindName("PART_Toolbar", pdfViewer) as DocumentToolbar;

It always return null and every code behind this one will be 'Object not set to a reference object'. Can everyone help me to solve this?


1 Reply

MS Manigandan Shanmugam Syncfusion Team October 3, 2023 11:10 AM UTC

Hi Nguyen,

We tried to reproduce the issue on our end with provided information, but we were unable to reproduce the reported issue. We suspect that the problem arises when we obtain the document toolbar instance in a constructor, so please ensure this is done in the Window_loaded method. Please refer the below code snippet and attached sample for your reference.

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            //Get the instance of the toolbar using its template name.

            DocumentToolbar toolbar = pdfViewer.Template.FindName("PART_Toolbar", pdfViewer) as DocumentToolbar;

            //Get the instance of the file menu button using its template name.

            ToggleButton FileButton = (ToggleButton)toolbar.Template.FindName("PART_FileToggleButton", toolbar);

            //Get the instance of the file menu button context menu and the item collection.

            ContextMenu FileContextMenu = FileButton.ContextMenu;

            foreach (MenuItem FileMenuItem in FileContextMenu.Items)

            {

                //Get the instance of the save as menu item using its template name and disable its visibility.

                if (FileMenuItem.Name == "PART_SaveAsMenuItem")

                    FileMenuItem.Visibility = Visibility.Collapsed;

                //Get the instance of the print menu item using its template name and disable its visibility.

                if (FileMenuItem.Name == "PART_PrintMenuItem")

                    FileMenuItem.Visibility = Visibility.Collapsed;

            }

        }


Attachment: sample_c5152c0b.zip

Loader.
Up arrow icon