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
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
I understood and your example is very helpful.
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.
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?
Let me get straight to your questions.
- "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.
- "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