Parent-Child relation in treeview

2 posts, 0 answers
  1. Ivan
    Ivan avatar
    13 posts
    Member since:
    Aug 2011

    Posted 27 Jan 2012 Link to this post

    This is a simplified version of my class

    public class Item
    {
    int _id;
     string _name;
    Item _parentItem;
    ObservableCollection<Item> _childItems;
    }

    The thing I want to do is to have them in a treeview acording to their relationship. And this is how I did it.

    <telerikNavigation:RadTreeView
       ItemsSource="{Binding TreeItems,Converter={StaticResource HierarchyConverter}}"
       ItemTemplate="{StaticResource TreeTemplate}"/>
     
    <converter:HierarchyConverter x:Name="HierarchyConverter"/>
    <telerik:HierarchicalDataTemplate
       x:Key="TreeTemplate"
        ItemsSource="{Binding Converter={StaticResource HierarchyConverter}}">
              <TextBlock Text="{Binding Name}"/>
    </telerik:HierarchicalDataTemplate>

    And I also use this converter

    public class HierarchyConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                // We are binding an item
                TreeItem item = value as TreeItem;
                if (item != null)
                {
                    return item.Children;
                }
     
                ObservableCollection<TreeItem> items = value as ObservableCollection<TreeItem>;
                if (items != null)
                {
                    return items;
                }
                return null;
            }
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    I only get the first "level" and see the arrows next to the name but they won't expand so that I don't know if I did everything correct.

    Any help?

    Edit:

    Added this code

    <telerik:ContainerBindingCollection x:Key="TreeViewItemContainerBinding">
               <telerik:ContainerBinding PropertyName="IsExpanded" Binding="{Binding Path=IsActive, Mode=TwoWay}"/>
               <telerik:ContainerBinding PropertyName="IsSelected" Binding="{Binding Path=IsActive}"/>
           </telerik:ContainerBindingCollection>

    But now everything is expanded and when I click on an item it doesnt look like it's selected... it is just "flashing"



  2. Ivan
    Ivan avatar
    13 posts
    Member since:
    Aug 2011

    Posted 27 Jan 2012 Link to this post

    Ignore the question...

    After I used the code (with the edited part) everything worked fine after I took care that the service which was providing me the data was called only once :)
  3. DevCraft banner
Back to Top