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?
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
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)
{
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.
Awesome! I totally missed that (obviously. Thank you so much for the explanation. It really helps.
Most welcome, please get back to us for any further assistance.
- 5 Replies
- 2 Participants
- Marked answer
-
KA Keith A Price
- Feb 5, 2024 10:59 PM UTC
- Feb 8, 2024 05:52 AM UTC