Nodes with different values

6 posts, 0 answers
  1. Jonathan Miller
    Jonathan Miller avatar
    57 posts
    Member since:
    Jun 2008

    Posted 09 Sep 2008 Link to this post

    I'm sure this is simple and I'm missing something.

    I have a TreeView that I am populating from WCF, SL2b2, returing a List<>, with ID, FolderName, ParentID.

    Most everything is in codebehind.

    Question: How can I populate the radTreeView with different text and value variables?  For example

    ID      NAME
    1        Inbox
    2        Outbox
    3        Sent

    When a user clicks on Inbox, I want to populate a datagrid based on the radTreeView.SelectedValue.
  2. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 10 Sep 2008 Link to this post

    Hello Jonathan,

    Correct me if I'm wrong: you are data binding the treeview and you are not creating the treeview items with code, right? When RadTreeView is data bound its SelectedItem property will contain the real data item from the data source, so you will not need the SelectedValue property. Handle the SelectionChanged event of RadTreeView and depending on the SelectedItem, populate the DataGrid.

    The SelectedValue property works in conjunction with SelectedValuePath property. The later gets the path to the property from a data item, which value will be placed in SelectedValue. For example, in your case you need to set SelectedValuePath="ID" and SelectedValue will contain the value of the ID property of the data item, that is bound to the currently selected RadTreeViewItem.

    I hope this helps.

    Regards,
    Valeri Hristov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Jonathan Miller
    Jonathan Miller avatar
    57 posts
    Member since:
    Jun 2008

    Posted 10 Sep 2008 Link to this post

    Thank you, that is helping.

    2 questions please,

    When I am binding to my datasource from WCF, the items in my TreeView list the source, but not the actual values.  For example, I have set the

    radTree.DisplayMemberPath = "FolderName";

    but the TreeView shows every node exactly as "pcsSilverlight.pcsServiceReference.Folders"

    which is from
     [ServiceContract]  
        public interface IpcsService  
        {  
            [OperationContract]  
            List<Folders> GetFolders();  
        }  
     
        [DataContract]  
        public class Folders  
        {  
            [DataMember]  
            public string FolderName;  
     
            [DataMember]  
            public int ID;  
     
            [DataMember]  
            public int ParentFolderID;  
        } 

    So, question 1 is, what should I set the DisplayMemberPath to?

    (simple, I'm sure) Question 2, if I need to populate the nodes in code behind, how do I create a node with different display text and values

    i.e. radTree.Items.Add(lstFolders[i].FolderName);   but assign a different selected value

    Thanks, great product!
  4. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 15 Sep 2008 Link to this post

    Hello Jonathan,

    The TreeView would not automatically create a hierarchy based on a ParentID and ID fields.

    One approach is to add a collection of the child items to each Folders object and then use a DataTemplate like in this example:

    http://www.telerik.com/help/silverlight/hierarchical-data.html

    For your second question: If you create the TreeViewItems in code behind, i.e. you create each of them separately, then you can set the Header property of the item to whatever you want to be displayed in it (FolderName) while its Items would be the sub-folders.

    Do come back to us if you need more help with the data templates or the TreeView.

    Regards,
    Miroslav
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. Jonathan Miller
    Jonathan Miller avatar
    57 posts
    Member since:
    Jun 2008

    Posted 21 Nov 2008 Link to this post

    Hi, using this example:

    RadTreeView tree = sender as RadTreeView; 
     
     // get the clicked Item 
     RadTreeViewItem clickedItem = e.OriginalSource as RadTreeViewItem; 
     
     // add the new items 
     RadTreeViewItem newItem = new RadTreeViewItem() { Header = "New Item" }; 
     clickedItem.Items.Add(newItem); 

    With a manual add, how can I set and later access a different value?

    For this example, if I have added the item with the Header = "New Item", how can I set the value to "1" and retrieve "1" when SelectionChanged later?  Thanks 
  6. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 21 Nov 2008 Link to this post

    Hello Jonathan Miller,

    for this scenario you can use the Tag property to store that data you want ( in your case the "1" digit).

    Kind regards,
    Valentin.Stoychev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top