我有带复选框的树视图,想要访问树视图中子节点的子节点。
这就是我制作树视图的方式,我想访问 level4 项目检查和取消选中值,以便我可以插入数据库。
private void FillingTreeDynamic()
{
Nodes.Clear();
var data = dbobj.GetDataFromDataBase("", "Select * from Master");
foreach (DataRowView dr in data)
{
var level_1_items = new Node() { Text = dr["MasterName"].ToString(),IsChecked=false};
var details = dbobj.GetDataFromDataBase("", "Select * from Details where MasterID='"+Convert.ToInt32(dr["ID"].ToString())+"'");
foreach(DataRowView row in details)
{
var level_2_items = new Node() { Text = row["SubName"].ToString(),IsChecked=false};
level_2_items.Parent.Add(level_1_items);
level_1_items.Children.Add(level_2_items);
var level_3_items = new Node() { Text ="View",IsChecked=false };
level_3_items.Parent.Add(level_2_items);
level_2_items.Children.Add(level_3_items);
var dataView = dbobj.GetDataFromDataBase("","select * from ViewDetails");
foreach (DataRowView rowview in dataView)
{
var level_4_items = new Node() {Text=rowview["RightsName"].ToString(), IsChecked = false };
level_4_items.Parent.Add(level_3_items);
level_3_items.Children.Add(level_4_items);
}
}
Nodes.Add(level_1_items);
}
treeView.ItemsSource = Nodes;
}
我能够访问子节点及其值,如下所示。
public ObservableCollection<Node> Nodes { get; private set; }
foreach (var parent in Nodes)
{
if (parent.IsChecked == true)
{
foreach (var item in parent.Children)
{
if (item.IsChecked == true)
{
}
else
{
}
}
else
{
}
}
}
我如何访问子节点的 parent.Children。知道如何访问这些子节点。提前致谢。