ContextMenu

5 posts, 1 answers
  1. Seth
    Seth avatar
    5 posts
    Member since:
    Oct 2012

    Posted 19 Jun 2013 Link to this post

    How do I add a ContextMenu for the PDFViewer?

    I've tried this;
    <telerik:RadContextMenu.ContextMenu>
                        <telerik:RadContextMenu DataContext="{Binding ElementName=pdfViewer, Path=Commands}">
                            <telerik:RadMenuItem Header="Copy" Command="{Binding CopyCommand}"/>
                            <telerik:RadMenuItem Header="Select All" Command="{Binding SelectAllCommand}"/>
                            <telerik:RadMenuItem Header="Print..." Command="{Binding PrintPdfDocumentCommand}"/>
                        </telerik:RadContextMenu>

    But the menu items don't do anything.

    Thanks.
  2. Seth
    Seth avatar
    5 posts
    Member since:
    Oct 2012

    Posted 20 Jun 2013 Link to this post

    I see the winForms version of pdfViewer has the contextMenu by default.

    What am I missing to make this work with WPF?
  3. UI for WPF is Visual Studio 2017 Ready
  4. Answer
    Alexander
    Admin
    Alexander avatar
    41 posts

    Posted 21 Jun 2013 Link to this post

    Hi Seth,

    The DataContext must be set inside the RadPdfViewer or another parent element in order to be inherited by the RadContextMenu correctly, because RadContextMenu is represented in a different visual tree.
    I am attaching a sample project which demonstrates how to create RadContextMenu inside RadPdfViewer.

    I hope this is helpful. Please, do not hesitate to contact us if you have any other questions.

    Regards,
    Alexander
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  5. armando
    armando avatar
    2 posts
    Member since:
    Jan 2015

    Posted 30 Jan 2015 in reply to Alexander Link to this post

    Hi alexander, 
    your solution does not work when the pdf viewer is binded to a viewmodel.
    In my case DocumentSource is binded to a viewmodel property and if I set the datacontext as per your example I am not able to show the document any more

     <telerik:RadPdfViewer x:Name="pdfViewer"  DocumentSource="{Binding PdfSource,                                                                     Converter={StaticResource PdfDocumentSourceValueConverter}}"
                                                          Style="{DynamicResource RadPdfViewerStyle}"
                                                          Visibility="{Binding Path=IsVisible,
                                                                               Converter={StaticResource BoolToVisibilityConverter}}"


  6. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 04 Feb 2015 Link to this post

    Hello Armando,

    Thank you for the interest in RadPdfViewer.

    Here is an attached property that will set the proper DataContext to the context menu.

    public static class PdfViewerContextMenuHelper
    {
        public static readonly DependencyProperty ContextMenuProperty =
            DependencyProperty.RegisterAttached("ContextMenu", typeof(RadContextMenu), typeof(PdfViewerContextMenuHelper), new PropertyMetadata(null, new PropertyChangedCallback((s, e) => { ContextMenuChanged((RadPdfViewer)s, (RadContextMenu)e.NewValue); })));
     
        public static RadContextMenu GetContextMenu(DependencyObject obj)
        {
            return (RadContextMenu)obj.GetValue(ContextMenuProperty);
        }
     
        public static void SetContextMenu(DependencyObject obj, RadContextMenu value)
        {
            obj.SetValue(ContextMenuProperty, value);
        }
     
        private static void ContextMenuChanged(RadPdfViewer radPdfViewer, RadContextMenu contextMenu)
        {
            contextMenu.InheritDataContext = false;
            RadContextMenu.SetContextMenu(radPdfViewer, contextMenu);
            contextMenu.DataContext = radPdfViewer.CommandDescriptors;
        }
    }

    You can use it from Xaml like this:
    <telerik:RadPdfViewer  ...>
        <l:PdfViewerContextMenuHelper.ContextMenu>
            <telerik:RadContextMenu>
                      ...
            </telerik:RadContextMenu>
        </l:PdfViewerContextMenuHelper.ContextMenu>
     </telerik:RadPdfViewer>

    Hope this helps.

    Regards,
    Kammen
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for WPF is Visual Studio 2017 Ready