Sjors Miltenburg
Sjors Miltenburg

Reputation: 2730

preselect checkboxes in radtreeview silverlight)

I have a silverlight app where there is a telerik radtreeview with checkboxes. The user selects stuff and when the user wants to edit it's selection i need to pre-populate the tree with the previously saved selection.

I found out that I can bind the checkboxes to my viewmodel. But if I choose that scenario I don't use the "built in" checkboxes and lose the tristate logic (autoselecting siblings when selecting a parent and such)

So I am experimenting with trying to get the radtreeviewitem objects from the radtreeview.items collection

http://www.telerik.com/help/silverlight/radtreeview-how-to-iterate-through-treeviewitems.html

The problem is that the radtreeviewitems are only generated when a node is expanded by a user in the ui. So not all items I want to iterate through are present after the control is databound.

I have not found a good way to force the ui to build all the radtreeviewitems so I can iterate through them and set my preselection. I found the links below but it only seems to work with the root node, not the siblings.

WPF: control.ItemContainerGenerator.Status is NotStarted. How do I tell it to start now?

Would you guys also consider rebuilding the "tristate-mode" into your viewmodel logic "dirty"?

How would you go about preselecting checkboxitems in the radtreeview?

Upvotes: 0

Views: 531

Answers (2)

Pavel Pavlov
Pavel Pavlov

Reputation: 767

When working with the RadTreeView control you need to have in mind that the built-in tri-state logic is designed to work with declaratively defined control and items, only. This means that using this feature in MVVM scenarios will not work as expected.

Since Telerik is aware of that limitation they provided the community with an article demonstrating how developers can use the tri-state logic of a native CheckBox control in MVVM scenarios. You can find the article in their documentation. Also, at the end of the article you can find a link leading to their CodeLibrary where you can download ready to run project demonstrating the described approach.

I hope this information will help you.

Upvotes: 0

Oliver
Oliver

Reputation: 817

This is how I do it :

 public static void CheckAllTreeItemsAuto(RadTreeView tree)
        {
            tree.ItemContainerGenerator.StatusChanged += (s, e) =>
            {
                if ((s as Telerik.Windows.Controls.ItemContainerGenerator).Status == Telerik.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
                {
                    RadTreeViewItem item = (RadTreeViewItem)tree.ItemContainerGenerator.ContainerFromIndex(0);
                    while (item != null)
                    {
                        item.IsChecked = true;
                        item = item.NextItem;
                    }
                }

            };

        }

I didn't experience your problem with the items not generated at the start. (I don't know how you generate your RadTreeView).

Upvotes: 0

Related Questions