wpf treeview 数据绑定 递归绑定节点

1.先上效果

将所有节点加入ComboBox数据源,在ComboBox中选择时下方Treeview显示该节点下的子节点。

wpf treeview 数据绑定 递归绑定节点

1.xaml文件,将以下代码加入界面合适位置

     <StackPanel>
<StackPanel Margin="10">
<Label Content="选择组节点:"></Label>
<ComboBox MaxDropDownHeight="100" Name="cmbGoup" DropDownClosed="cmbGoup_DropDownClosed"></ComboBox>
</StackPanel>
<StackPanel Margin ="10">
<TreeView x:Name="tvGroup">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
<StackPanel>
<TextBlock VerticalAlignment="Center" FontSize="14" Text="{Binding GroupName}" Margin="2,0,0,0"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</StackPanel>
</StackPanel>

2.后台代码

a.用于绑定的节点类

     public class Group
{
public Group()
{
this.Nodes = new List<Group>();
this.ParentId = ;//主节点的父id默认为0
} public List<Group> Nodes { get; set; }
public int ID { get; set; }//id
public int ParentId { get; set; }//parentID
public string GroupName { get; set; }
}

b.主界面类代码

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); #region 用于绑定的数据
List<Group> grpLst = new List<Group>(){
new Group(){ID=,GroupName="Group", ParentId = -},
new Group(){ID=,GroupName="Group1",ParentId=},
new Group(){ID=,GroupName="Group2",ParentId=},
new Group(){ID=,GroupName="Group1_1",ParentId=},
new Group(){ID=,GroupName="Group1_2",ParentId=},
new Group(){ID=,GroupName="Group1_3",ParentId=},
new Group(){ID=,GroupName="Group1_4",ParentId=},
new Group(){ID=,GroupName="Group1_5",ParentId=},
new Group(){ID=,GroupName="Group2_1",ParentId=},
new Group(){ID=,GroupName="Group2_2",ParentId=},
new Group(){ID=,GroupName="Group2_3",ParentId=},
new Group(){ID=,GroupName="Group2_4",ParentId=},
new Group(){ID=,GroupName="Group1_1_1",ParentId=},
new Group(){ID=,GroupName="Group1_1_2",ParentId=},
new Group(){ID=,GroupName="Group1_2_1",ParentId=},
new Group(){ID=,GroupName="Group1_1_1_1",ParentId=}
};
#endregion this.cmbGoup.ItemsSource = grpLst;//comboBox数据源
this.cmbGoup.SelectedValuePath = "ID";
this.cmbGoup.DisplayMemberPath = "GroupName"; List<Group> lstGroup = getTreeData(-, grpLst);//初始化时获取父节点为-1的数据
this.tvGroup.ItemsSource = lstGroup;//数据绑定
} /// <summary>
/// 递归生成树形数据
/// </summary>
/// <param name="delst"></param>
/// <returns></returns>
public List<Group> getTreeData(int parentid, List<Group> nodes)
{
List<Group> mainNodes = nodes.Where(x => x.ParentId == parentid).ToList<Group>();
List<Group> otherNodes = nodes.Where(x => x.ParentId != parentid).ToList<Group>();
foreach (Group grp in mainNodes)
{
grp.Nodes = getTreeData(grp.ID, otherNodes);
}
return mainNodes;
} /// <summary>
/// 下拉框关闭事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmbGoup_DropDownClosed(object sender, EventArgs e)
{
if (this.cmbGoup.SelectedValue == null)
{
return;
}
int groupId = (int)this.cmbGoup.SelectedValue;//选中的组号
List<Group> lstGroup = getTreeData(groupId, (List<Group>)cmbGoup.ItemsSource);
this.tvGroup.ItemsSource = lstGroup;
}
}
上一篇:maven 向本地私库导入jar


下一篇:Bandwagon的配置记录(一) —— kexue上网