This is a migrated thread and some comments may be shown as answers.

Nodes with different values

5 Answers 111 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Jonathan Miller
Top achievements
Rank 1
Jonathan Miller asked on 09 Sep 2008, 08:08 PM
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.

5 Answers, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 10 Sep 2008, 07:25 AM
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.
0
Jonathan Miller
Top achievements
Rank 1
answered on 10 Sep 2008, 12:46 PM
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!
0
Miroslav
Telerik team
answered on 15 Sep 2008, 08:34 AM
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.
0
Jonathan Miller
Top achievements
Rank 1
answered on 21 Nov 2008, 01:04 PM
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 
0
Valentin.Stoychev
Telerik team
answered on 21 Nov 2008, 04:27 PM
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.
Tags
TreeView
Asked by
Jonathan Miller
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
Jonathan Miller
Top achievements
Rank 1
Miroslav
Telerik team
Valentin.Stoychev
Telerik team
Share this question
or