Check / Uncheck TreeView Dynamically

4 posts, 1 answers
  1. Ahmet Özgür
    Ahmet Özgür avatar
    37 posts
    Member since:
    Jul 2009

    Posted 16 Dec 2009 Link to this post

    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.
  2. Answer
    Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 16 Dec 2009 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Ahmet Özgür
    Ahmet Özgür avatar
    37 posts
    Member since:
    Jul 2009

    Posted 16 Dec 2009 Link to this post

    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.
  5. Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 16 Dec 2009 Link to this post

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