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

Check / Uncheck TreeView Dynamically

3 Answers 72 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Ahmet Özgür
Top achievements
Rank 1
Ahmet Özgür asked on 16 Dec 2009, 06:23 AM
Hello,

I have a treeview control and want to check the nodes from code dynamically according to the values coming from the database. Below is my xaml
<telerik:RadTabItem.Content> 
      <telerik:RadTreeView Name="TreeViewCategories" SelectionMode="Extended" 
            IsLineEnabled="True" ItemsOptionListType="CheckList" IsOptionElementsEnabled="True" 
            IsRootLinesEnabled="True" IsTriStateMode="True" > 
            <telerik:RadTreeView.ItemTemplate> 
                  <HierarchicalDataTemplate ItemsSource="{Binding Path=ChildCategories, Mode=TwoWay}">  
                         <StackPanel>                                          
                                <TextBlock  Text="{Binding Name}" Name="textblockName"></TextBlock> 
                         </StackPanel> 
                  </HierarchicalDataTemplate> 
            </telerik:RadTreeView.ItemTemplate> 
      </telerik:RadTreeView> 
</telerik:RadTabItem.Content> 

I try your solution at this page
http://www.telerik.com/community/forums/wpf/treeview/how-to-check-uncheck-items-from-code-behind.aspx

But could not resolve it. When i try the method mentioned at the solution it gives an error. The error comes from the if statement
"Object reference not set to an instance of an object". Below is the code.
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)  
        {  
            if ((sender as RadTreeView).ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)  
            {  
                foreach (RadTreeViewItem item in TreeViewCategories.Items)  
                {  
                    item.CheckState = System.Windows.Automation.ToggleState.On;  
                }  
            }  
        } 

Can you suggest a solution please.

Regards.

3 Answers, 1 is accepted

Sort by
0
Accepted
Bobi
Telerik team
answered on 16 Dec 2009, 09:37 AM
Hello Ahmet Özgür,

Please find attached a sample project that shows how to accomplish the desired functionality.
I have prepared a sample project that demonstrates a possible solution to your scenario.
In my business object, I have 3 properties (IsChecked, IsExpanded and IsSelected) that are used to control the 3 corresponding properties of RadTreeViewItem - CheckState, IsExpanded and IsSelected.
The important thing is the following binding:
<telerikNavigation:RadTreeView.ItemContainerStyle>
                <Style TargetType="{x:Type telerikNavigation:RadTreeViewItem}">
                    <Setter Property="CheckState"
                            Value="{Binding IsChecked, Mode=TwoWay, Converter={StaticResource CheckStateConverter}}" />
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                </Style>
            </telerikNavigation:RadTreeView.ItemContainerStyle>

I hope that this will help you. Please let us know if you have any other questions or need some more help.

Sincerely yours,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Ahmet Özgür
Top achievements
Rank 1
answered on 16 Dec 2009, 12:41 PM
Thank you for your solution. It gave as a good pathway for our project. We created a partial class of our table which we used to bind the treeview. we added the items ischecked,isselected,isexpanded and used your convertor.
0
Bobi
Telerik team
answered on 16 Dec 2009, 02:17 PM
Hi Ahmet Özgür,

I am glad to hear that I managed to help you.
Please do not hesitate to ask us if you have any other questions.

Sincerely yours,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
TreeView
Asked by
Ahmet Özgür
Top achievements
Rank 1
Answers by
Bobi
Telerik team
Ahmet Özgür
Top achievements
Rank 1
Share this question
or