Make an item from list to be a parent node.

8 posts, 2 answers
  1. Son
    Son avatar
    13 posts
    Member since:
    Sep 2013

    Posted 07 Dec 2015 Link to this post

    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.

  2. Answer
    Dinko
    Admin
    Dinko avatar
    201 posts

    Posted 10 Dec 2015 Link to this post

    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 hierarchical 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 http 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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Son
    Son avatar
    13 posts
    Member since:
    Sep 2013

    Posted 10 Dec 2015 Link to this post

    Thank you so much for explaining, Dinko. 
    I understood and your example is very helpful. 
  5. Son
    Son avatar
    13 posts
    Member since:
    Sep 2013

    Posted 23 Dec 2015 Link to this post

    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.

  6. Son
    Son avatar
    13 posts
    Member since:
    Sep 2013

    Posted 27 Dec 2015 Link to this post

    excute me, is everyone in holidays and nobody on duty? 
  7. Son
    Son avatar
    13 posts
    Member since:
    Sep 2013

    Posted 28 Dec 2015 Link to this post

    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? 

  8. Answer
    Dinko
    Admin
    Dinko avatar
    201 posts

    Posted 28 Dec 2015 Link to this post

    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 i get "AP" before debug."  - 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
  9. Son
    Son avatar
    13 posts
    Member since:
    Sep 2013

    Posted 25 Jan in reply to Dinko Link to this post

    Thank you once more, Dinko, your project helped me much. I'm sorry because of this lately reply.
Back to Top
UI for WPF is Visual Studio 2017 Ready