ViewModel for XAML Treeview

ViewModel for XAML Treeview

Old forum URL: forums.lhotka.net/forums/t/7976.aspx


kevnworking posted on Wednesday, November 11, 2009

Hey All,

I was hoping someone could push me in the right direction here or, at least, confirm I am going in the right direction.  I have a treeview in my XAML application.  I had the treeview bound to a child collection and working great.  I noticed that when I would perform a save on the parent object the treeview would collapse back to the root.  I don't like this behavior as I believe the treeview should return to the same "expanded" state as it was in before the save was performed.

I began researching a solution to this issue and it appears to me that a ViewModel is the solution I am looking for.  I think I can create a ViewModel for the child collection and another view model for the individual child objects, then add an "IsExpanded" property to the child object ViewModel and databind that to the "IsExpanded" property of the TreeViewItem.

I assume with a two way binding on the IsExpanded property of the treeview that the treeview will remain expanded as the user had it.

Does this sound like a reasonable solution?  I'm using version 3.8.0 of the CSLA framework in preparation of creating ViewModel objects.

Kevin

kevnworking replied on Thursday, November 12, 2009

OK.  Created the viewmodel classes by looking at Rocky's example.  I have the ViewModel's bound to the treeview and am getting data again.  I have a question.  In Rocky's example the Data and DataList classes seem to be stand-alone classes, in other words they aren't children of another class, which is not the circumstances of the two classes/viewmodels I am working with.  So this may be oversimplifying my question a bit but how do I associate the ViewModel classes I just created with what used to be the BusinessObject's parent class?  In other words, the BO/VM's I just created are no longer associated with what used to be the BO's parent class (because I am now creating the BO's in the VM's and not in the parent class) so now the save and cancel buttons in my XAML are not functioning properly.

Thanks for your patience with me, it's probably evident that I am just getting started with this...

Kevin

Copyright (c) Marimer LLC