WPF Tree Parent Child Insertion:
I am trying to create Below parent child structur using WPF tree.
Tree
->Parent
->Child
->Grand Child.
I have written the below code which is not able to insert for child. Please help me to fix this.
<Window x:Class="NewTree_DynamicNode.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView Name="treeFileSystem" TreeViewItem.Expanded="treeFileSystem_Expanded_1">
开发者_如何学JAVA <TreeViewItem Header="Categories" x:Name="_ImageTree" Tag="hi"
x:FieldModifier="private">
<TreeViewItem TextBlock.FontStyle="Italic"
Header="Loading..." Name="treeFileSystem2"/>
</TreeViewItem>
</TreeView>
</Grid>
</Window>
private void treeFileSystem_Expanded_1(object sender, RoutedEventArgs e)
{
this._ImageTree = (TreeViewItem)e.OriginalSource;
this._ImageTree.Items.Clear();
try
{
for(int i=0 ; i<2; i++)
{
TreeViewItem temp = new TreeViewItem();
TreeViewItem temp1 = new TreeViewItem();
temp.Header = "Parent";
temp1.Header = "Child";
temp.Items.Add(temp1);
this._ImageTree.Items.Add(temp);
}
}
catch
{
/////
}
}
Your problem is that every time you expand a node, the child node for the node you expand is always 'Parent
'
Im not sure exactly what you are trying to achieve but heres some code:
TreeViewItem temp = new TreeViewItem();
temp.Header = "Child";
temp.Items.Add(null);
this._ImageTree.Items.Add(temp);
EDIT - this is for specific names in hierarchy
TreeViewItem temp = new TreeViewItem();
var header = string.Empty;
switch (_ImageTree.Header.ToString())
{
case "Categories":
header = "Parent";
break;
case "Parent":
header = "Child";
break;
case "Child":
header = "GrandChild";
break;
default:
header = "Child of " + _ImageTree.Header;
break;
}
temp.Header = header;
temp.Items.Add(null);
this._ImageTree.Items.Add(temp);
精彩评论