how to bind data to display hierarchy (visual basic)

3 posts, 0 answers
  1. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 21 Oct 2011 Link to this post

    Hi there,

    I'm having difficulty figuring out how to bind my data ( ObservableCollection(of) ) and to display it as a hierarchy using vb.

    I have the following example data, similar to your C# example I saw in another post called Treelistview-Self-Hierarchy.

    ID             Name               ParentID
    1               John                 NULL                   ** Top level node **
    2               Chris                1                      ** Parent is John **
    3               Simon                NULL                   ** Top level node **
    4               Dave                 1                      ** parent is John **
    5               Joe                  3                      ** parent is simon **

    ** Please note again that the above is an ObservableCollection(Of ) list.

    I have the following XAML List view.

    <telerik:RadTreeListView x:Name="ParksTreeListView" AutoGenerateColumns="False" IsFilteringAllowed="True" IsBusy="False">
        <telerik:RadTreeListView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding ID}"
                                Header="ID" />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"
                                Header="Name" />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding ParentID}"
                                Header="Parent" />
        </telerik:RadTreeListView.Columns>
    </telerik:RadTreeListView>

    I then set the ItemsSource in VB:

    'Get parks information from SQL database
    Dim parksList As ObservableCollection(Of ParksTree) = ParksDB.GetAllParks()
      
    'Set Tree items source
    ParksTreeListView.ItemsSource = parksList

    This displays all the information correctly in grid. However I need to group the items according to it's hierarchy.How can I achieve this with the minimal of fuss?
    ** Please note I am a newbie! **

    Out of interest, the hierachy in my SQL 2008 database uses the new hierarchyid data type. Can the TreeListView use the hierarchyid data directly to generate the tree nodes?
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 24 Oct 2011 Link to this post

    Hello,

     You need to create new property for your data item representing the child items and bind this property using TreeListViewTableDefinition. 

    Please check our demos and documentation for more info!

    Greetings,
    Vlad
    the Telerik team

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

  3. UI for WPF is Visual Studio 2017 Ready
  4. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 24 Oct 2011 Link to this post

    I take it this is where I should be looking?

    Namespace Telerik.Windows.Examples.TreeListView.OnDemandDataLoading
        Public Class FolderViewModel
            Inherits ViewModelBase
            Private m_name As String
            Private m_isEmpty As Boolean
            Private m_items As ObservableCollection(Of FolderViewModel)
            Private folderElement As XElement
      
            Public Sub New(name As String, isEmpty As Boolean, element As XElement)
                Me.Name = name
                Me.IsEmpty = isEmpty
                Me.folderElement = element
            End Sub
      
            Public Property Name() As String
                Get
                    Return Me.m_name
                End Get
                Private Set
                    Me.m_name = value
                End Set
            End Property
      
            Public Property IsEmpty() As Boolean
                Get
                    Return Me.m_isEmpty
                End Get
                Private Set
                    Me.m_isEmpty = value
                End Set
            End Property
      
            Public Property Items() As ObservableCollection(Of FolderViewModel)
                Get
                    Return Me.m_items
                End Get
                Private Set
                    If Me.m_items IsNot value Then
                        Me.m_items = value
      
                        OnPropertyChanged("Items")
                    End If
                End Set
            End Property
      
            Public Sub LoadChildren()
                If Me.Items Is Nothing Then
                    Me.Items = New ObservableCollection(Of FolderViewModel)(From f In Me.folderElement.Elements("folder") _
                        Select New FolderViewModel(f.Attribute("name").Value, CBool(f.Attribute("isEmpty")), f))
                End If
            End Sub
        End Class
    End Namespace

    Am I right in thinking that "items" are actually another observable collection list contained in a column of the parent list?
    Interesting, it never occurred to me to embed a list inside another list column.

Back to Top