在我的程序中,我想获取父节点的所有子节点,以便我可以向其添加新节点。过去我使用这种方法:
//This gets all the children of a parent node depending on it's DisplayName
var node = ObservableCollection.GetAllChildren(x => x.Children).Distinct().ToList().First(x => x.DisplayName == nameOfNode);
node.Children.Add(CreateNode(newNodeName));
现在我想根据我的 SelectedItem
属性GetAllChildren
,它是完全正常的。是否可以使用 GetAllChildren
方法来做到这一点?如果是,怎么办?
补充 1:
我的 SelectedItem
属性是 ViewModel 类型。 ObservableCollection
也是 ViewModel
类型,所以基本上 SelectedItem
告诉程序选择了哪个 ViewModel。
补充 2:
我不能简单地添加到父节点。在添加之前,我必须检查这个程序中有多少 child 因为其他原因。
最佳答案
我可能是错的,但对我来说,您似乎在问如何找到等于 SelectedItem 的节点并向其添加新节点。你为什么不直接使用 SelectedItem.Add 呢???它会为您节省按引用搜索的次数。
要对 child 进行计数,只需使用 Linq Count() 方法:)
关于c# - 如何获取 SelectedItem 的所有子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21711164/