Please help me solve this code smell with Wpf/MVVM and a tree view control

I've got an base view model representing a 'FileSystem' type, which is either an Directory, File, or Drive (Fixed/Network currently).

I'm trying to put all the logic in this base view model for the IsExpanded, IsChecked, and IsSelected properties of a tree view control/item. As the logic for these functionalities are the same no matter the file system type.

I have this odd feeling i'm doing the ischecked evaluation portion incorrectly. But cannot for the life of me wrap my head around a better way. I'm not trying to change it (cause it does work) for performances, as i doubt it is much. it's just doesn't feel like it's the most effective way of doing this task.

So basically i have BaseFileSystemViewModel class with the logic for; Expanding, Checking, and Selecting an item. I also have logic for finding checked items and lazy loading the children items in here. The code for that is here:

An example of one of the inheriting classes of the BaseFileSystemViewModel class is here:

Please note: My IFileSystemViewModel interface is basic, just forcing the requirement of a few properties, such as Name, FullName, Length (aka file size). So i didn't include the code here, as it's straight forward. But can provide it, if it's needed.

Can someone please tell me if they see a better way of doing this? Thank you for your time, as I've been banging my hand on the table trying to unravel this problem.

Edit: helps if i include the problem…

My problem is the recursive changing in IsChecked property setter. It goes up the chain (parents) and then back down the chain (children), and in certain cases repeats one of the two. Such as if you check a middle item, it goes up parents, then down children and then back up parents again. But i need the parents to update when a child value is changed, recursively. Also i need children values to change when a parent is checked/changed recursively. Hopefully that explanation makes sense !

by socmonkey05 via /r/csharp

Leave a Reply