I have some code I am asked to convert from Windows Forms over to WPF project. One thing I have run into is with TreeNodes being different.
This is the orginal windows forms code.
private void CreateTagTree()
{
StringBuilder sb = new StringBuilder();
// get list of plc program tags
ABLink.Program plcProgram = myPLC.ProgramList[0];
string[] plcInfo = (string[])cpuInfo.Value;
sb.Append(plcInfo[0]+Environment.NewLine).Append(plcInfo[1] + Environment.NewLine).Append("Mode: " + plcInfo[2] + Environment.NewLine).Append("Status: " + plcInfo[3]);
rootNode.Nodes.Clear();
rootNode.Text = plcProgram.Name;
rootNode.ToolTipText = sb.ToString();
// add root node
treeView1.Nodes.Add(rootNode);
// get list of plc tag items
ReadOnlyCollection<TagTemplate> DataTableList = plcProgram.TagItems();
// create a node for each tag Template
foreach (TagTemplate tagTemplate in DataTableList)
{
TreeNode tagNode = new TreeNode(tagTemplate.Name);
tagNode.Tag = tagTemplate;
AddNode(rootNode, tagTemplate);
}
}
////////
// adds a tag node to tree
private void AddNode(TreeNode parent, ABLink.TagTemplate template)
{
TreeNode node = new TreeNode(template.Name);
parent.Nodes.Add(node);
// attache template to node
node.Tag = template;
node.ToolTipText = template.TypeName;
ReadOnlyCollection<TagTemplate> memberList = template.Members;
// if template has members
foreach (ABLink.TagTemplate member in memberList)
AddNode(node, member);
}
The above example works great and no issues with it. But trying to upgrade this program for more room to scale it in the future.
This is what I have so far and it is not displaying the treeview correct. It is leaving a lot of blank items and I am not sure why.
private void CreateTagTree()
{
if (PLC5Radio.IsChecked == true)
{
//treeview1.Items.Add(rootNode);
StringBuilder sb = new StringBuilder();
// get list of plc program tags
ABLink.Program plcProgram = myPLC.ProgramList[0];
string[] plcInfo = (string[])cpuInfoAB.Value;
sb.Append(plcInfo[0] + Environment.NewLine).Append(plcInfo[1] + Environment.NewLine).Append("Mode: " + plcInfo[2] + Environment.NewLine).Append("Status: " + plcInfo[3]);
rootNode.Items.Clear();
rootNode.Header = plcProgram.Name;
rootNode.ToolTip = sb.ToString();
// add root node
//treeview1.Items.Add(rootNode);
TreeViewItem prgNode = new TreeViewItem
{
Header = plcProgram.Name
};
//// get list of plc tag items
ReadOnlyCollection<ABLink.TagTemplate> DataTableList = plcProgram.TagItems();
// get collection of TagItems in
// filters "*" returns all tag names & atomic types
tagListAB = plcProgram.TagItems(filtrName.Text, filterType.Text);
// create a node for each tagTemplate
foreach (ABLink.TagTemplate tagTemplate in tagListAB)
{
TreeViewItem tagNode = new TreeViewItem();
tagNode.Items.Add(tagTemplate.Name);
rootNode.Tag = tagTemplate;
AddNodeAB(rootNode, tagTemplate);
}
}
private void AddNodeAB(TreeViewItem parent, ABLink.TagTemplate template)
{
TreeViewItem node = new TreeViewItem();
node.Items.Add(template.Name);
parent.Items.Add(node);
// attache template to node
node.Tag = template;
node.ToolTip = template.TypeName;
ReadOnlyCollection<ABLink.TagTemplate> memberList = template.Members;
// if template has members
foreach (ABLink.TagTemplate member in memberList)
AddNodeAB(node, member);
}
Here is an image of what I am getting currently. Working program is on the right; my non working program is on the left with the extra spaces added in.
I believe part of the issue has to do with this
TreeNode tagNode = new TreeNode(tagTemplate.Name);
But with the changes in WPF I am not sure the best way to correct it. Any advice on what to be doing would be helpful.
in WinForms
TreeNode tagNode = new TreeNode(tagTemplate.Name);
creates a TreeNode and sets Text value.
WPF code
TreeViewItem tagNode = new TreeViewItem();
tagNode.Items.Add(tagTemplate.Name);
creates an empty TreeViewItem and adds a child. That is NOT an equivalent operation, obviously.
replacement for Text will be Header
property:
TreeViewItem tagNode = new TreeViewItem();
tagNode.Header = tagTemplate.Name;
tagNode.Tag = tagTemplate;
and similarly in the next method
TreeViewItem node = new TreeViewItem();
node.Header = template.Name;
parent.Items.Add(node);
Header
has object
type and can contain any value, not just plain text, for example, another control. Please study this CodeProject article to get the overview: Basic Understanding of Tree View in WPF .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.