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

3 posts, 0 answers
  1. Kalle
    Kalle avatar
    26 posts
    Member since:
    Feb 2013

    Posted 14 Apr 2013 Link to this post

    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. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 16 Apr 2013 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Kalle
    Kalle avatar
    26 posts
    Member since:
    Feb 2013

    Posted 23 Apr 2013 Link to this post

    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
Back to Top