This is a migrated thread and some comments may be shown as answers.

Allow only one TreeViewItem to be checked at a time per node / treeview

2 Answers 224 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Kalle
Top achievements
Rank 1
Kalle asked on 14 Apr 2013, 06:45 PM
Hi!

What would be best way to allow only one TreeViewItem to be checked at a time per Node? Or even better only one item checked in whole TreeView at a time. When new item is checked the previous would be unchecked first. I know it's possible to make it in code-behind but I'm trying to find the best solution for implementing it.

Something like first unchecking whole treeview in PreviewChecked event and then checking the new item (or would it be enough to just uncheck everything)? 

Thanks!

Br,

Kalle

2 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 16 Apr 2013, 08:15 AM
Hello Kalle,

The best way to implement your desired scenario would be to use data binding to your view-model. This of course would be appropriate if you are using data-bound TreeView.

If we assume you are using a data-bound TreeView, the steps would be like follows:
1) Place a check box inside your  TreeViewItems data template.
2) Create an IsChecked property in your view-model objects.
3) Use a style binding with two way mode between IsChecked property of the data template check box and IsChecked property in your view model.
4) In the view model IsChecked property setter make a call to function that unchecks all other items and leave the current one checked.
This way you could encapsulate your business logic inside your view model and test it easily eliminating the concerns related to RadTreeView.

However, if RadTreeView is xaml defined and you are not using data binding probably you should use code behind.

Hope this helps. Please let us know if you need more info.

Kind regards,
Hristo
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Kalle
Top achievements
Rank 1
answered on 23 Apr 2013, 10:31 AM
Hi,

I was able to get this done using data binding and IsChecked property in the ViewModel. Thank you very much for the help :)

Br,

Kalle
Tags
TreeView
Asked by
Kalle
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Kalle
Top achievements
Rank 1
Share this question
or