Question

I have a TreeView in my program that needs to be implemented from the Data Model level of an MVVM structure. It needs to be implemented this way so that the program saves it in data. My program switches between different windows and the tree is lost during those switches if it only goes as deep as the View Model level. I have the data working, but the view does not reciprocate (a problem that I seem to be having a lot lately).

I will post my process here. Please keep in mind the structure of this TreeView:

-Usercontrol(contains TreeView)->ViewModel->Model(TreeView needs to be stored in this Model)

-TreeView->ViewModel->Model

UserControl Model:

public BlockingTreeViewModel _blockDataTree;

public BlockingDatabaseModel() { }

public BlockingTreeViewModel BlockDataTree {...}

UserControl View Model:

public BlockingDatabaseModel _blockingModel;

public BlockingDatabaseViewModel(BlockingDatabaseModel model, BlockingTreeViewModel blockingTreeView)
{
    BlockingModel = model;
    BlockingModel.BlockDataTree = blockingTreeView;
}

public BlockingDatabaseModel BlockingModel {...}
public BlockingTreeViewModel BlockingTree //PROXY PROPERTY
{
    get { return BlockingModel.BlockDataTree; }
    set
    {
        BlockingModel.BlockDataTree = value;
        NotifyPropertyChange(() => BlockingTree);
    }
}

UserControl.xaml:

<TreeView ItemsSource="{Binding BlockingModel.BlockDataTree.BlockTree}" ... />

Now, upon adding to this TreeView I can see the new node when I debug, but the View does not show any newly created node. As far as my understanding of this situation, this is implemented correctly and should work fine. Please help me with why the view will not show this tree inside the UserControl.

Now that the above changes have been applied to my xaml the tree nodes are created and displayed. However, these nodes disappear whenever I change the screen. This means that there is still something wrong with my TreeView in this MVVM structure.

*Note: The property for the TreeView is a proxy property only because it makes my code simpler in other areas of my program. This should not affect what I am doing though.

Update 1:

Screenshot of my "Locals" window during debugging. This shows that I am able to create a new node in the TreeView.

enter image description here

Update 2: Updated bindings in xaml

Was it helpful?

Solution

If you are switching screens that contain TreeViews you shouldn't create a new ObservableCollection every time you view the TreeView because it will reset the current one.

I say this because I was creating a new ObservableCollection in the ViewModel of my TreeView everytime I switched back to it.

constructor of my TreeViewViewModel:

public BlockingTreeViewModel()
{
    BlockTree = new ObservableCollection<BlockingTreeModel>();
}

Instead...:

//Pass the ObservableCollection from a different area of your code each time
//you use BlockingTreeViewModel so that you don't overwrite it.
public BlockingTreeViewModel(ObservableCollection<BlockingTreeModel> treeCollection)
{
    BlockTree = treeCollection;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top