Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hai i am new to WPF csharp so i dont want to use MVVM concepts i am not clear whether we use INotifyPropertyChanged in treeview then we need to follow mvvm.because i need to do check boxes if i check parent node all the other child node also need to check.


What I have tried:

i tried upto tree view all the nodes are visible i stuck in checkboxes functionalities
Posted
Updated 20-Jan-21 4:15am

There are a number of ways to implement checkboxes (and nothing to do with MVVM); this one might be the least abstract.

TreeView In WPF[^]

(And "checking all children" when the parent is checked is a "business rule"; you don't actually have to go around "checking" everything underneath; you use the rule later for whatever).
 
Share this answer
 
v2
Comments
Fazil13 20-Jan-21 9:00am    
yes,but there is no code for when i select check box in the root node it should affect the child node.
[no name] 20-Jan-21 9:04am    
I added that .. pointless gymnastics. If the user changes their mind, at least they keep their original "child" selections. The alternative is starting all over; trying to remember what you checked in the first place. This is usually what the "programmer" thinks; not the user.
You should build your tree structure up though the ViewModels - i.e. a ViewModel instance for each item.

You can then databind the checkbox to this viewmodel (which in turn can be listening to events from the model).

This means your "IsChecked" (or whatever you call the property) set method will be called when the user change the state of the checkbox.
In this code. you can recursively go through your child items and change their state as well. DataBinding will then automatically make sure the checkbox of the child items updates as expected. You can also databind things like the item being expanded or selected - then you can just change this property from code, and the view will react automatically.

In some cases it makes sense updating the child items in the ViewModel, in other cases you would just pass it down to the model - and it would do the recursive set/clear of it's IsChecked property... and then the ViewModels would react in the PropertyChanged event listener to update the ViewModel which in return updates the view. This is basically a question of where the reasoning for the child checkboxes beeing set/cleared is. If it is a domain issue, I would make the domain model do the logic. If it is pure userinterface, then it is the viewmodel.

If you add or remove items in your tree, you should indeed implement INotifyCollectionChanged (you can use ObservableCollection) for the collection holding an items childitems.

You always need INotifyPropertyChanged (and INotifyCollectionChanged, though you can usually get away with just using ObservableCollection). It is one of the more tedious things with MVVM. Overall worth it compared to WinForm etc in my opinion, but do not try using MVVM without - then you just get the worst of both worlds.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900