Load model from file to DiagramViewModel

I am working on a solution to instantiate a DiagramViewModel loaded with a model prior to showing it on the UI. In my code, I instantiate the view model and pass in a file name. The goal is to load the xaml file and then pass the view model to the controller hosting the SFDiagram control. I have created the following code but the Info object is null thereby throwing an error.


How can I properly load a view model from the file? The only objects I can find that support loading model xaml from a file are the SFDiagram itself and the above IGraphInfo. 

The overall goal is a MVVM pattern where I can pass a populated view model to a user control and then assign the Data Context from the view model to the SFDiagram control. Any assistance is very much appreciated. 


3 Replies

KR Karkuvel Rajan Shanmugavel Syncfusion Team January 18, 2022 11:22 AM UTC

Hi Stephen,  
  
Requirement : DiagramViewModel serialization and binding  
  
We have prepared a simple sample for your requirement. In the sample we have serialize a ViewModel in button click and save it in a xml file. Then while button click for each diagram we have load the viewmodel and set the viewmodel as datacontext for respective SfDiagram. Please find the sample and demo video links below.  
  
  
  
Regards,  
Karkuvel Rajan S 



SS Stephen Shake January 21, 2022 12:34 AM UTC

Thank you for the reply and the samples. I have gone through them and while I understand you are recommending serializing and deserializing the diagram objects manually, I have some additional concerns. My diagrams will be very complex with connectors, dynamic ports, colors and custom decorators (see the screenshot for a sample). If I need to custom write all of that, it will be a massive undertaking. Especially since the SFDiagram.Save will handle all of this complexity already. I have used the SFDiagram.Save() and SFDiagram.Load() and it properly captures all of the complexities saving to a file and then reloading. I just need a way to leverage these methods from the View Model instead of from the control. 




DT Deepa Thiruppathy Syncfusion Team January 21, 2022 12:09 PM UTC

Hi Stephen,


Requirement : How to load model from file to View-Model


We have analysed your requirement. We could see that you are trying to load a file from the View-Model’s constructor itself. The value for View-Model's Info property will initiated only after its dependent diagram view is being loaded. So, we are requesting you to load file to View-Model once its dependent diagram view is loaded. We have created a simple sample in which we have added separate commands for performing Save and Load in the View-Model.


Please find the sample link as below,


https://www.syncfusion.com/downloads/support/directtrac/general/ze/DiagramSaveLoadFromViewModel1110269323



If the given sample does not meet your requirement, then please let us know the purpose of loading diagrams file from the View-Model. We will try to achieve your requirement depending on your purpose.


Regards,

Deepa Thiruppathy


Loader.
Up arrow icon