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

RadTreeView and XML Datasource with TAG values.

1 Answer 18 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Bodhi
Top achievements
Rank 1
Bodhi asked on 20 Jan 2021, 08:40 PM

I am following the following documentation for the RadTreeView on populating a Treeview through an XML Datasource.

However, The documentation doesn't really get in depth about how the XML schema and naming conventions correlates to the RadTreeview control itself or how the corresponding XML elements are being linked up.

The ID and parentID elements are obvious enough and those do appear to be referenced in the initialization of the RadTreeview control through the following code.

 

this.radTreeView1.ChildMember = "id";
this.radTreeView1.ParentMember = "parentId";

 

However, the XML "Title" element maps to the RadTreeView.DataMember property, which is not as obvious.

A RadTreeNode seems to have 4 different properties available to populate (Name, Tag, Text, Value). Again based on documentation I am not sure what the difference is in each of these values.

When populating the RadTreeView through XML Datasource, and then iterating through each of the nodes, it seems as though (Name, Text, and Value) are all populated with the XML value from the "Title" element, and that the RadTreeNode (Tag) value is left blank.

Can someone elaborate on the difference between (Name, Tag, Text, and Value) and what specific purpose each is intended to serve?

Can someone also elaborate on how the XML elements are being mapped?

Additionally can the XML schema be modified to populate these values? I am particularly interested in populating the (Tag) property, but would like to understand what else is possible with XML.

Right now I can iterate through the RadTreeView nodes after its loaded and manually set the Tag value, but this kind of defeats the purpose of using something like XML in the first place.

Any help much appreciated!

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 22 Jan 2021, 10:18 AM

Hello James,

I will get straight to your questions using as an example the documentation article that you have pointed. I will paste the xml and radTreeView setup from the documentation. 

In this example we use the following xml:

    <Toc>
      <Node>
        <Title>Main Title</Title>
        <id>1</id>
        <parentId></parentId>
      </Node>
      <Node>
        <Title>Child Title</Title>
        <id>2</id>
        <parentId>1</parentId>
      </Node>
    </Toc>

And we have the following RadTreeView setup.

DataSet tocDataSet = new DataSet("Toc");
tocDataSet.ReadXml("TreeView\\DataBinding\\toc.xml");
this.radTreeView1.DataMember = "Node";
this.radTreeView1.DisplayMember = "Title";
this.radTreeView1.ChildMember = "id";
this.radTreeView1.ParentMember = "parentId";
this.radTreeView1.DataSource = tocDataSet;

Using this example, I will start by addressing how the XML elements are mapped to the radTreeView data fields.

XML "Title" element maps to the RadTreeView.DisplayMember field, not to DataMember. DisplayMember is the field that populates the Text property of each node.  I will address all XML elements from this example and their mapping to radTreeView data fields below:

XML element RadTreeView data field
Node DataMember
Title DisplayMember 
id ChildMember
parentId ParentMember 


You asked about the following RadTreeNode properties and the difference in their values: Name, Tag, Text, Value.

Name - Gets or sets the name of the RadTreeNode
Text - Gets or sets the text to be used as label text. As mentioned above, this value represents the DisplayMember.
Note that you can access a specific Node by Name or Text.

Tag - Gets or sets the tag object that can be used to store user data, corresponding to the tree node.
Value - Gets or sets the node value. This value represents the ValueMember.

I would like to note that Tag field is general for all TPF controls. It is not specific to the RadTreeView dataBinding. Tag is of type Object and it allows you to store any type of data that you need. In the case of a RadTreeNode your approach to populate it programmatically is correct. You cannot populate data inside it via the XML itself.

However, it is important to note what kind of information you want to store for the different RadTreeView nodes. If you want to populate text data from your XML you can use a different approach from the TagIn the case of dabinding to XML your ValueMember field is populated with the same data as Text and Name. However, you can Map this field to a specific XML element.

For example, you can add xml element <info>: 

<info>Some info text 1.</info>	
this.radTreeView1.ValueMember = "info"; 

Doing so will allow you to store text data for different nodes that can later be accessed via the Value property. 

If this approach suits your scenario, please note that you will have to set TreeViewElement.EditMode property to Text.

this.radTreeView1.TreeViewElement.EditMode = TreeNodeEditMode.Text;

This will ensure that when you perform a Node edit, only the Text will be edited and the Value will remain the same. 

Just a side note, EditMode property has the following values:

Value: When the user performs edit, the Value will be edited.
Text: When the user performs edit, the Text will be edited.
TextAndValue (by default): When the user performs edit, both the Text and the Value will be changed.

You can find more information in this article.

I hope you find this information useful.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Treeview
Asked by
Bodhi
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or