Saving/Restoring TreeView with custom items in heirarchy

3 posts, 1 answers
  1. Scott
    Scott avatar
    66 posts
    Member since:
    Nov 2009

    Posted 26 Sep 2011 Link to this post

    I have a TreeView that is using a HierarchicalDataTemplate and ContainerBindingCollection. These use a class I created to display the items in the TreeView. I'm saving the stream of the TreeView to a database. Whn I restore the TreeView I am not getting any items expanded or checked. When I do the same with TreeViewItems declared in the TreeView itesself (in XAML) ir works fine. Do I need to use a custom ICustomPropertyProvider to do this?

    My TreeView style:
    <Style x:Key="RadTreeViewSelectExpandedStyle" TargetType="telerik:RadTreeView" BasedOn="{StaticResource RadTreeViewStyleBase}">
    <Setter Property="IsOptionElementsEnabled" Value="True"/>
    <Setter Property="IsTriStateMode" Value="True" />
    <Setter Property="ItemTemplate">
    <Setter.Value>
    <telerik:HierarchicalDataTemplate ItemsSource="{Binding Children}" telerik:ContainerBinding.ContainerBindings="{StaticResource TreeViewItemCheckedExpandedBindingCollection}">
    <TextBlock Text="{Binding Value}" />
    </telerik:HierarchicalDataTemplate>
    </Setter.Value>
    </Setter>
    </Style>

    My ContainerBindingCollection
    <telerik:ContainerBindingCollection x:Key="TreeViewItemCheckedExpandedBindingCollection">
    <telerik:ContainerBinding Binding="{Binding Path=CheckState, Mode=TwoWay}" PropertyName="CheckState" />
    <telerik:ContainerBinding Binding="{Binding Path=Expanded, Mode=TwoWay}" PropertyName="IsExpanded" />
    </telerik:ContainerBindingCollection>
  2. Answer
    Alex Fidanov
    Admin
    Alex Fidanov avatar
    636 posts

    Posted 29 Sep 2011 Link to this post

    Hello Scott,

    The PersistenceFramework works only with FrameworkElements and their properties. As you have bound the treeview control, its Items collection holds your business objects rather than the treeviewitem containers. That is why, the settings of the items is not persisted.

    You could implement an ICustomPropertyProvider and save the state of the business objects - for example the IsSelected, IsChecked (CheckState), IsExpanded properties, etc., that you have bound with ContainerBinding. When loading, you would have to match the saved objects with the ones in the ItemsSource, so I would recommend using an identifier (much like the GridView persisting demo and the GridViewColumns).

    Please let me know if you have questions on this or if you need further assistance.

    Kind regards,
    Alex Fidanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Scott
    Scott avatar
    66 posts
    Member since:
    Nov 2009

    Posted 29 Sep 2011 Link to this post

    Thanks for the confirmation. That is what I thought the answer was going to be.
Back to Top