Wednesday, October 31, 2012

TreeViewItem with CheckBox - Selection

TreeViewItem with CheckBox - Selection

Hi

I have a treeview and TreeViewItems are added in the treeview.

I need to have a checkbox in each treeviewitem and if i select/check any of the treeviewitem, all childs for selected treeviewitems should be selected/checked.

If i unselect, all child items also unselected.

If i unselect any one of child item and parent item is selected then parent item should be unselected.

Please let me know how can i achieve this.

 

 

Answers & Comments...

Answer: 1

I assume you are binding to some property like "IsChecked" on your tree nodes?  In the setter for taht property, you just need to add the logic to set/unset the appropriate nodes.

1 - You'll have to make your checkboxes tri-state...  (True / False / Indeterminate) to represent this properly.

2 - When setting your current node to true:  set all descendants to true; then set all ancestors to either true or indeterminate (need to evaluate all their descendants to decide)

3 - When setting your property to false - set all descendants to false, then set all ancestors to false or indeterminate (again, you need to evaluate their children to decide)

I'm sure there are more scenarios to this than I am thinking of, but this should get you started at least...



Answer: 2

Hi Cleon,

Thanks for your response.

Actually we are creating treeview custom control and modify the styles and templates.

In treeviewitem control template, i added the checkbox which is mentioned below.

<CheckBox x:Name="CheckBoxElement" Grid.Column="2" IsTabStop="False" Margin="5,0,0,0" Visibility="Visible" VerticalAlignment="Center" />

In onapplytemplate, i get the checkbox object and implement the checkall and uncheckall functionality.

But i could not get the child item checkbox object intially when parent item is in collapsed state due to virtualization. so i unable to check/uncheck the child item check box object when parent item is checked/unchecked.

and also when i uncheck any one of child item when parent item is checked then parent item checkbox should be unselected and also we check parent checkbox and select the expand icon to expand the parent item. here how can we provide child item checkbox selection behaviours.

thanks

suyambu

 

 


 

 





No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog