WPF Dockable Pane and MVVM with Multiple Documents Open

WPF Dockable Pane and MVVM with Multiple Documents Open

jasonprichardtcc
Contributor Contributor
1,554 Views
2 Replies
Message 1 of 3

WPF Dockable Pane and MVVM with Multiple Documents Open

jasonprichardtcc
Contributor
Contributor

I have a conceptual question for those who are experienced with WPF dockable panes with MVVM design pattern.  

 

I have an application that has the following sequence:

WPF modeless dockable pane stores selection data via text boxes and comboboxes as properties for an application.

The user selects(combobox) or inputs(textbox) the desired properties.

The user clicks a command button on my custom Ribbon.

The application retrieves the information from the viewmodel, and performs the function based on the viewmodel data.

 

To date (which I knew was wrong from the beginning....), I have been storing the viewmodel data in a Singleton instance so that I can consistently retrieve the information from the Revit command.  The WPF form is databound to the instance so they remain in sync.  This works great provided there are not two different Revit Documents open in the application window.  As soon as I move between two different documents, the data in the viewmodel does not stay in sync with the document that I'm working in (obviously....)

 

I feel certain I can work this out by storing Revit Document instances and associated viewmodel instances as documents are open using a DocumentOpeneded event.  However, I am curious for those who have doing this much longer than I have if there is a good standard of practice / pattern I should consider to ensure viewmodel data can stay in sync with the current Document.

 

Thank you,
Jason

 

 

0 Likes
Accepted solutions (1)
1,555 Views
2 Replies
Replies (2)
Message 2 of 3

RPTHOMAS108
Mentor
Mentor

I would use instances with an active instance for the viewmodel set after UIApplication.ViewActivated.

 

I would only store in the viewmodel non-Revit items or ElementIds to Revit items i.e. I would re-fetch the Revit data from the model not store it out of context.

 

Document.GetHashCode can be used to link the right instance of your viewmodel to the active document. If you are using viewmodel instances then setting one as the main DataContext will save a lot of binding work. Not sure you could so easily achieve the same if it were a static object.

 

In this example

<TextBlock Text="{Binding Source={x:Static pi:ProgramInfo.ProgramName}}" />

Reduces to

<TextBlock Text="{Binding ProgramName}" />

If overall DataContext points to for example 'ActiveViewModel' instance or static property returning an instance of type viewmodel.

Message 3 of 3

jasonprichardtcc
Contributor
Contributor
Accepted solution

Thank you for the quick response.  The ViewActivated event was key to what I need to know how to update the document data, and I hadn't considered using the GetHashCode as the reference key.  I do not believe going down the static object route is ultimately going to be beneficial, but helpful to know down the road.

0 Likes