Load document via Command with MVVM

I see that your wpf controls are designed to work with MVVM and you have an example using TabControlExt.

But I can't find specific documentation for other controls that explains how to bind all the properties and events (commands) using MVVM.

Specifically, I need to convert Load and Save and PageClick to Commands I can put in my ViewModel.

In the case of Load, I'm trying to move this line:
myViewer.Load(filePath); to the ViewModel.

If there's documentation, please point me to it.

A code sample with these specifics would be great.

But also some kind of general pattern I can use to figure out other cases.

For example, if you give me specifics on Load, can you explain the concept so I can figure out, without documentation if there isn't any, how I could accomplish the same thing for any event?


5 Replies 1 reply marked as answer

KG Krithika Ganesan Syncfusion Team February 6, 2024 02:07 PM UTC

To load the PDF in the WPF PdfViewer, you can utilize the ItemSource property following the MVVM pattern. Similarly, for saving the PDF, you can use the SaveDocumentCommand and we have shared a code snippet for this below. Although there isn't a specific command for the PageClicked event of the WPF PdfViewer, you can handle this event in the ViewModel class for MVVM pattern implementation.


We have attached a sample below demonstrating how to load the PDF and utilize the PageClicked event of the WPF PdfViewer in the MVVM pattern.


Additionally, here's a code snippet demonstrating how to use the SaveDocumentCommand:


pdfViewerControl.SaveDocumentCommand.Execute(@"C:\temp\Output.pdf");


Attachment: MVVMSample_12775099.zip

Marked as answer

KA Keith A Price February 6, 2024 04:19 PM UTC

Thanks so much for that!

I'm not seeing where loaded is wired up to the pdfviewer in either the xaml or the view model:

        /// <summary>
        /// Initializes control in loaded event.
        /// </summary>
        public void Loaded(object sender, RoutedEventArgs e)
        {


KG Krithika Ganesan Syncfusion Team February 7, 2024 02:09 PM UTC

In the sample we provided previously,


You can see that the PdfViewerControl is inside the Grid. Within the Grid, we have set the WindowLoaded dependency property to true. As a result, the WindowLoadedChanged event will be triggered when we update the dependency property. In the WindowLoadedChanged event, we access the view model instance from the grid. Using the view model instance, we have wired up the loaded event in the sample as you specified.



KA Keith A Price February 7, 2024 04:57 PM UTC

Awesome! I totally missed that (obviously. Thank you so much for the explanation. It really helps.



KG Krithika Ganesan Syncfusion Team February 8, 2024 05:52 AM UTC

Most welcome, please get back to us for any further assistance.


Loader.
Up arrow icon