8

I'm using AvalonDock in a WPF application, and need to persist the layout of the AvalonDock the user has setup (moving, docking, detaching of the panels).

The function to do that are on the control itself (SaveLayout, RestoreLayout).

What is the correct way to do that ?

Right now I have a command at the mainWindowViewModel that gets created by the application when creating the instance of the window view and the viewmodel. It basically makes the relay command call an anonymous method that calls the needed function on the view control. This works since the application creates the window and the viewmodel for it.

But how would i approach this if some lower level view and viewmodel had to do this? If using this method I'd have to still create the Command at application level and sending it through the viewModels down to where it is needed to be bound to? Inside it I'd have to search the usercontrol that is a view then the avalonDock control inside it and use that at application level, which is bug prone. Somehow it seems dirty...

Thanks!

Marino Šimić
  • 7,318
  • 1
  • 31
  • 61

2 Answers2

3

You might introduce an interface IView so that the ViewModel can call a method on the View. This way the ViewModel doesn’t need to know about the concrete View class and can still be unit tested.

How this can be accomplished is shown by the sample applications of the WPF Application Framework (WAF).

jbe
  • 6,976
  • 1
  • 43
  • 34
1

You can use decoupled messaging to communicate between view models

http://www.bradcunningham.net/2009/11/decoupled-viewmodel-messaging-part-1.html

Navid Rahmani
  • 7,848
  • 9
  • 39
  • 57
  • Message subscription seems cool. Here I would have all viewModels publish messages where the main app would subscribe to. However that solves only part (the least important) of my problem. I still have the problem on how to approach calling the methods from some controls in some view under the main window view (iside it). Or you perhaps have another way to make that work with this? – Marino Šimić Jun 18 '11 at 11:07
  • Can you explain a little bit more – Navid Rahmani Jun 18 '11 at 11:28
  • Imagine we are working in a company that has many sites and one of them provides user controls. Some of those usercontrols has an AvalonDock control inside it to have docking managed internally. If I wanted to save the layout of its own AvalonDock how would I do that? I could possibly not have the source code of that control either. But I would need to use it in one of my views, and need to save its AvalonDock layout when needed. – Marino Šimić Jun 18 '11 at 11:38
  • This is some improbable case. But it would be hard even if I had the source code. I would need to give the AvalonDock some x:name, the same for the usercontrol, and need to 'find' that at application launch to be able to call that method to save/load. – Marino Šimić Jun 18 '11 at 11:40
  • You can search for the AvalonDock control, look [here](http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls/1759923#1759923) – Navid Rahmani Jun 18 '11 at 11:45
  • Navid, that solution probably works, but seems overkill since i could just use FrameworkElement.FindName(), but I like more the idea that jbe gave. I will use IDockableView on the view, giving those methods inside and pass that to the viewmodel. Seems as a better solution. – Marino Šimić Jun 19 '11 at 15:44