带CheckBox的WPF TreeView

编程入门 行业动态 更新时间:2024-10-25 15:23:01
本文介绍了带CheckBox的WPF TreeView-如何获取已检查列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经在此处通过复选框实现了TreeView 我表现得很好。现在,我想知道从树中选择/选中项目的列表。我相信我需要编写一个递归方法。但是作者已将递归方法 GetTree()设置为静态方法,但我没有任何线索开始递归。 也可以找到类似的文章此处,我找到了解决问题的方法也到那边去,但没有任何帮助。

I have implemented TreeView with Checkboxes from here I shows perfectly well. Now I am wondering to get list of selected/checked items from the tree. I believe I need to code a recursive method. But the author has set the recursive method GetTree() to be Static and I can't get any clue to start the recursion. Similar article is also found here, I found in a way for my problem overe there also, but couldn't get any thing helpful.

我在此处附上完整的代码供您参考:

I am attaching whole code here for your reference :

public class TreeViewModel : INotifyPropertyChanged { public TreeViewModel(string name) { Name = name; Children = new List<TreeViewModel>(); } #region Properties public string Name { get; private set; } public List<TreeViewModel> Children { get; private set; } public bool IsInitiallySelected { get; private set; } bool? _isChecked = false; TreeViewModel _parent; #region IsChecked public bool? IsChecked { get { return _isChecked; } set { SetIsChecked(value, true, true); } } void SetIsChecked(bool? value, bool updateChildren, bool updateParent) { if (value == _isChecked) return; _isChecked = value; if (updateChildren && _isChecked.HasValue) Children.ForEach(c => c.SetIsChecked(_isChecked, true, false)); if (updateParent && _parent != null) _parent.VerifyCheckedState(); NotifyPropertyChanged("IsChecked"); } void VerifyCheckedState() { bool? state = null; for (int i = 0; i < Children.Count; ++i) { bool? current = Children[i].IsChecked; if (i == 0) { state = current; } else if (state != current) { state = null; break; } } SetIsChecked(state, false, true); } #endregion #endregion void Initialize() { foreach (TreeViewModel child in Children) { child._parent = this; child.Initialize(); } } public static List<TreeViewModel> setTree(TreeViewModel tree) { List<TreeViewModel> treeView = new List<TreeViewModel>(); treeView.Add(tree); tree.Initialize(); return treeView; } public static List<TreeViewModel> SetTree(string topLevelName) { List<TreeViewModel> treeView = new List<TreeViewModel>(); TreeViewModel tv = new TreeViewModel(topLevelName); treeView.Add(tv); //Perform recursive method to build treeview #region Test Data //Doing this below for this example, you should do it dynamically //*************************************************** TreeViewModel tvChild4 = new TreeViewModel("Child4"); tv.Children.Add(new TreeViewModel("Child1")); tv.Children.Add(new TreeViewModel("Child2")); tv.Children.Add(new TreeViewModel("Child3")); tv.Children.Add(tvChild4); tv.Children.Add(new TreeViewModel("Child5")); TreeViewModel grtGrdChild2 = (new TreeViewModel("GrandChild4-2")); tvChild4.Children.Add(new TreeViewModel("GrandChild4-1")); tvChild4.Children.Add(grtGrdChild2); tvChild4.Children.Add(new TreeViewModel("GrandChild4-3")); grtGrdChild2.Children.Add(new TreeViewModel("GreatGrandChild4-2-1")); //*************************************************** #endregion tv.Initialize(); return treeView; } public static List<string> GetTree() { List<string> selected = new List<string>(); //select = recursive method to check each tree view item for selection (if required) return selected; //*********************************************************** //From your window capture selected your treeview control like: TreeViewModel root = (TreeViewModel)TreeViewControl.Items[0]; // List<string> selected = new List<string>(TreeViewModel.GetTree()); //*********************************************************** } #region INotifyPropertyChanged Members void NotifyPropertyChanged(string info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public event PropertyChangedEventHandler PropertyChanged; #endregion }

我找不到合适的使用 SetTree(string topLevelName)方法从我的代码中动态地获取树,因此我添加了 setTree(TreeViewModel tree)适合我需求的方法。

I couldn't find appropriate to se the tree dynamically from my code using SetTree(string topLevelName) method, hence I added setTree(TreeViewModel tree) method to suit my needs.

谁能帮助我实现GetTree()方法来获取已检查项目列表。如果选择了树的项,则TreeViewModel的IsChecked属性设置为true。因此,可以使用IsChecked查找是否选择了该项目。

Can anyone please help me to implement GetTree() method to get list of items that is checked. If an item of the tree is selected, the TreeViewModel's IsChecked property is set to true. So IsChecked can be used to find if the item is selected r not.

推荐答案

获取树中所有选中的节点在某个节点以下并包含,请在该节点上调用以下 GetCheckedItems 方法:

To get all checked node in your tree below and including a certain node, call the following GetCheckedItems method on that node:

private IEnumerable<TreeViewModel> GetCheckedItems(TreeViewModel node) { var checkedItems = new List<TreeViewModel>(); ProcessNode(node, checkedItems); return checkedItems; } private void ProcessNode(TreeViewModel node, IEnumerable<TreeViewModel> checkedItems) { foreach (var child in node.Children) { if (child.IsChecked) checkedItems.Add(child); ProcessNode(child, checkedItems); } }

更多推荐

带CheckBox的WPF TreeView

本文发布于:2023-10-31 01:41:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CheckBox   WPF   TreeView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!