Binding view to content control issue.

Mar 29, 2011 at 1:16 AM

I got a dashboard that can potentially contain 1 or 2 graphs.  To do this I used the view model "context" ability in caliburn micro. 

class DashboardVM 
{
     public Graph0 { get; set; }
     public Graph1 { get; set; }
}
View 1: (1 graph dashboard)
<ContentControl Name="Graph0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />

View 2: (2 graph dashboard)
<ContentControl Name="Graph0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />

<ContentControl Name="Graph1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />
The problem is the way the view is binded to the contentcontrols.  After viewing graph 1, and going to the 2 graph view, graph 1 will be invisible from then on in the 2 graph view.

Any clever suggestions on how to resolve this?  Of course I know how to solve this in too many lines of code, I was looking for an elegant way.

Coordinator
Mar 29, 2011 at 1:46 AM

Are Graph0 and Graph1 inheriting from Screen?

Mar 29, 2011 at 2:34 AM

Yes.  (I am going to figure out why this question is relevant now...)

Mar 29, 2011 at 2:45 AM

I still haven't discovered why, but indeed NOT deriving from Screen fixes the problem.  

Very astute Eisenberg!

Coordinator
Mar 29, 2011 at 3:39 AM
Edited Mar 29, 2011 at 3:39 AM

Here's the brief explanation. Screen caches views. So, if you have a screen being visualized in two different places (without using contextual views), since the screen can only be in one place in the Visual Tree at once, some strange things can happen. If you want the functionality that comes with Screen, such as lifecycle and graceful shutdown, you can just override AttachView without calling the base. That will effectively turn off the caching. I'll have a better solution for this in the future. If you don't need Screen specific features, then I would just keep things lightweight and inherit from PropertyChangeBase.

Mar 29, 2011 at 4:00 AM

Thank you very much for the information.  If you can point me to one of your essays that talks about the view caching in detail, along with the "contextual views" you alluded to, I would be most grateful.  

If you haven't gotten around to writing about it, I understand.  At least I will eventually figure it out from reading the code.

I have one more question for you, I'll start another thread for it. 

Coordinator
Apr 3, 2011 at 1:16 AM

I've made a breaking change which actually improves this scenario for you if you need to inherit from Screen. Have a look at build 58d8998e0c91 and the few prior to that along with the accompanying comments.