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

Make an item from list to be a parent node.

7 Answers 124 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Son
Top achievements
Rank 1
Son asked on 08 Dec 2015, 04:32 AM

I read examples from docs.telerik.com, downloaded demos but they're often so long and i can not know what i need in them. 

I only need make an item of a list to parent items in code behind. Do i need HierarchicalDataTemplate or RadTreeViewItem ?

 

_______ my code _______

public static HttpClient client = new HttpClient() ;
private void _ProductList()
{
      var url = "api/Products/";
      HttpResponseMessage response = client.GetAsync(url).Result;

      if (response.IsSuccessStatusCode)
      {
         var x = response.Content.ReadAsAsync<List<Product>>().Result;
         Product allprod = new Product();
         allprod.ProdID = 0;
         allprod.Name = " All Products";

        x.Add(allprod);
        myTreeView.ItemsSource = x;
        myTreeView.DisplayMemberPath = "Name";
        myTreeView.SelectedValuePath = "ProdID";
        // ....
     }
     else
     {
        MessageBox.Show("Error Code " + response.StatusCode +
                    " : Message - " + response.ReasonPhrase);
     }
}

__________

I tried RadTreeViewItem, but it doesn't have SelectedValuePath property.

- Items show Name and selected value is its ProdID 
And it seems difficult to use HierarchicalDataTemplate in code behind (with me).

- With my code, i have "prod.png" and i need "need.png" 

It's the best if you can edit code to me, please. Thank you.

__________

At last, i think demos (not only treeview) need a version classic-demo which is the most simple (except styles) with small data.
Example "First look" is really a big demo.

7 Answers, 1 is accepted

Sort by
0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 10 Dec 2015, 04:52 PM
Hello Son,

Thank you for contacting us.

RadTreeView is working with hierarchical data, in order to display the Product object as in the "need.png" picture you will need to use structure for your models. In your case, you can add a property to the Product class which will hold the children collection and then set the collection of the "All products" item to the result returned by the response. This way you can add the products in the desired parent.

About the DisplayMemberPath property, it is inherited from the ItemsControl class and it will be applied only for the first level of the hierarchy. Instead, you can use hierarchical data template or item template selector with DataTemplates. You can find such approach demonstrated in the attached project.

As the complex examples, we have a GitHub repo  where you can find different simple projects for our control. 

Regards,
Dinko
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Son
Top achievements
Rank 1
answered on 11 Dec 2015, 03:53 AM
Thank you so much for explaining, Dinko. 
I understood and your example is very helpful. 
0
Son
Top achievements
Rank 1
answered on 24 Dec 2015, 03:37 AM

Hello, i want to update my question. How can i control the HierarchicalDataTemplate's TextBlock?

Example: With a list of Fruits  and Flowers which maybe have same ids because my source is in many databases

Fruit (ID, Name): ( 01, Apple), ( 02, Orange)... , with ( 0, All Fruits) is a treenode 

Flower (ID, Name): ( 01, Rose), ( 03, jasmine)... , with ( 0, All Flowes) is a treenode

-- DataTemplate shows Name in treeview and treeview.selectedvalue is ID 
-- Can I check selected Name to know it's of fruit or flower? 
___
An other example in images:
-- how can i get "AP" before debug. eg: something likes (treeview.SelectedItem).mota
with seleced item.png

-- or how to know ID of SelectedValue is of _List1 or _List2 ?

___
Thank you, Merry Chrismas and Happy new year.

0
Son
Top achievements
Rank 1
answered on 28 Dec 2015, 02:22 AM
excute me, is everyone in holidays and nobody on duty? 
0
Son
Top achievements
Rank 1
answered on 28 Dec 2015, 09:37 AM

At last i tried to bind SelectedItems[0].Name with ElementsName = treeview to a textbox. It worked. 

But do you have another way using treeview without adding more control? 

0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 28 Dec 2015, 01:16 PM
Hello Son,

Let me get straight to your questions.
  1. "Can I check selected Name to know it's of fruit or flower?" - You can create a additional property in the Product view model which will hold information about the group of the product. Then in the SelectionChanged event, you can check the selected item group name. 
  2. "How can get "AP" before ."  - You can cast the selected item to a Product and get the property:
Product selectedItem = xTreeView.SelectedItem as Product;
var txtMota = selectedItem.mota;

    3. "How to know ID of SelectedValue is of _List1 or _List2" - The solution is similar to the first question. Create a property which will hold the name of the list which the selected item is part of.

I have modified the project from my previous reply so you can see how to implement the mentioned above approaches.

Regards,
Dinko
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Son
Top achievements
Rank 1
answered on 25 Jan 2016, 02:57 PM
Thank you once more, Dinko, your project helped me much. I'm sorry because of this lately reply.
Tags
TreeView
Asked by
Son
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Son
Top achievements
Rank 1
Share this question
or