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

How to check/uncheck items from code behind?

6 Answers 813 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
HotlineOrbicon
Top achievements
Rank 1
HotlineOrbicon asked on 23 Feb 2009, 10:19 PM
I am working on my first WPF project and just started using RadControls for WPF so please forgive me if this is a stupid question :)

I have a RadTreeView with CheckBoxes (IsOptionElementsEnabled = "True" , ItemsOptionListType="CheckList")
In my code behind, I can see which items are checked or not using the "CheckedItems" property. Very nice.

However, how can I change the CheckBox status on individual items in my RadTreeView (check/uncheck) from code behind?
What is the best way to check/uncheck all checkboxes from code behind?

Kind regards,
Lars Bodin

 

 

 

6 Answers, 1 is accepted

Sort by
0
Tihomir Petkov
Telerik team
answered on 26 Feb 2009, 04:40 PM
Hi Lars,

You can set the CheckState property of RadTreeViewItems from the code-behind by attaching to the ItemContainerGenerator.StatusChanged event of the immediate parent(treeview for root items or treeview item for child items) of the item you want to change. (As a prerequisite you need to add UIAutomationTypes to the references of your project) Here's an example for changing root items:

treeView.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);

void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
 if ((sender as RadTreeView).ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
 {
  foreach (RadTreeViewItem item in treeView.Items)
  {
   item.CheckState = System.Windows.Automation.ToggleState.On;
  }
 }
}

However, it would be much easier (especially if you need to set the CheckState of all items) to make a wrapper class for your original data source. This wrapper class can simply be a list of TreeViewItems and in its constructor you can pass your original data items. Then, one by one you can create TreeViewItems and take whatever you need from the original data objects, as well as set the properties you want on the TreeViewItem.

I hope this short clarification helps you. Please do not hesitate to ask further questions if you still have difficulties.

Best wishes,
Tihomir Petkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Henrique Duarte
Top achievements
Rank 1
Veteran
answered on 30 Apr 2009, 10:05 AM
Hi Tihomir,

In my case, I need to enpand and check all child items from a node and the user double click an item.
I'm expanding and checking only the selected item, but how can I check the child items?
Here is my code:

        private void RadTreeView_MouseDoubleClick(object sender, MouseButtonEventArgs e)  
        {  
            RadTreeViewItem item = (RadTreeViewItem)((RadTreeView)(sender)).SelectedContainer;  
            if (item != null)  
            {  
                item.ExpandAll();  
                item.CheckState = System.Windows.Automation.ToggleState.On;  
            }  
        } 

PS1: Why don't you create a new method called ".CheckAll()" to check all child items from a node?
PS2: I cannot use the treeview on tri-state mode.

Regards,

Henrique
0
Valentin.Stoychev
Telerik team
answered on 01 May 2009, 01:42 PM
Hi Henrique Duarte,

You need to loop thru all the items of the selected RadTreeViewItem and to check them aswell. Here is your modified code:
private void RadTreeView_MouseDoubleClick(object sender, MouseButtonEventArgs e)     
{     
   RadTreeViewItem item = (RadTreeViewItem)((RadTreeView)(sender)).SelectedContainer;     
   if (item != null)     
   {     
      item.ExpandAll();     
      item.CheckState = System.Windows.Automation.ToggleState.On;  
      foreach (RadTreeViewItem subItem in item.Items)  
      {  
         subItem.CheckState = System.Windows.Automation.ToggleState.On;  
      }    
   }     
}    
 

To use the TreeView in tri-state modeset the IsTriStateMode property to True.

Kind regards,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Henrique Duarte
Top achievements
Rank 1
Veteran
answered on 01 May 2009, 07:54 PM

Hi Valentin,

Here is my scenario:

  • Company
    • Region A (Build 1)
      • Build 2
      • Build 3
      • Build 4
    • Region B (Build 5)
      • Build 6
      • Build 7
    • Region C (Build 8)
      • Build 9
      • Build 10
    • Region D (Build 11)

I want to generate a report for each build checked. I cannot use a tri-state mode because sometimes I need to generate a report for Build 1 only. To generate a report to entire Region A I was adding a MouseDoubleClick event to expand and check all child items and nodes. The foreach is not working for me. The Item is an HierarchyNode and not a RadTreeView. This is why I suggested you to add a CheckAll and SelectAll function. You already have an ExpandAll.
Do you have another suggestion?
I tried but I didn't found a way to get the child RadTreeViewItem.
If you prefer, I can open a support ticket for this.

Thank you once again!

Regards,

Henrique

0
Miroslav
Telerik team
answered on 04 May 2009, 11:26 AM
Hi Henrique Duarte,

Are you binding the TreeView? As I understand the problem is that you cannot always get the TreeViewItems. This is because the items are not created if not necessary and they will not be part of the checked items collection unless created.

I will suggest that you keep track of the checked nodes in your ViewModel (the items that you bind to). Then you can add the Checkbox in the HeaderTemplate of the TreeView. This way you will always have all the information you need, plus it will be a little bit more testable.

You will also be able to choose how IsChecked propagates to the children.

I tried to implement the above suggestion and it turned out that we have forgotten to bind the HeaderTemplateSelector property in the TreeViewItem ControlTemplate.

Currently a workaround will be to edit the default template of the RadTreeViewItem. I have done so in a sample project which I have attached to the reply.

Hopefully this approach will be flexible and powerful enough for your scenario.

Best wishes,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Henrique Duarte
Top achievements
Rank 1
Veteran
answered on 09 Jun 2009, 07:59 PM
Guys,

Since you don't have a CheckAll function to check all child nodes, I created a function to do it when user double click an item:

        private void RadTreeView_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
        { 
            RadTreeViewItem item = (RadTreeViewItem)((RadTreeView)sender).SelectedContainer; 
            if (item != null
            { 
                item.ExpandAll(); 
                item.UpdateLayout(); 
 
                if (item.CheckState == ToggleState.On) 
                    item.CheckState = ToggleState.Off; 
                else 
                    item.CheckState = ToggleState.On; 
 
                if (item.Items.Count > 0) 
                    UpdateItens(item.NextItem, item.CheckState); 
            } 
        } 
 
        private void UpdateItens(RadTreeViewItem item, ToggleState status) 
        { 
            item.CheckState = status; 
            if (item.Items.Count > 0) 
                UpdateItens(item.NextItem, status); 
             
            if (item.NextSiblingItem != null
                UpdateItens(item.NextSiblingItem, status); 
        } 

It's not a bug or question. I'm just sharing the code.

Best regards,

Henrique

Tags
TreeView
Asked by
HotlineOrbicon
Top achievements
Rank 1
Answers by
Tihomir Petkov
Telerik team
Henrique Duarte
Top achievements
Rank 1
Veteran
Valentin.Stoychev
Telerik team
Miroslav
Telerik team
Share this question
or