WPF的TreeView控件
天方 人气:0在WPF的TreeView使用方式和WinForm下有很大不同,那些展开某节点、获取父节点,判断某节点是否被选中等常用的操作在WinForm下都有相关函数,而在WPF中却不能轻易实现。
一种常规的方式是通过MVVM模式来将TreeViewItem节点中的IsSelect,IsExpanded等属性来双向绑定到要显示的节点数据中,然后直接通过节点数据的属性来实现相关操作。
但是,有的时候,当我们没有ViewModel层,但又想像WinFrom那样直接简单的获取或设置这些属性的时候,该如何办呢。其实WPF还是提供了类似WinForm中的这些设置的,只不过形式不一样了而已,但是却没WinFrom的那么直观和方便。CodeProject上就有人将常用函数总结了一下,写成了扩展函数,主要提供如下功能:
public static void SelectObject(this TreeView treeView, object obj) public static void SelectObject(this TreeView treeView, object obj, bool selected) public static bool IsObjectSelected(this TreeView treeView, object obj) public static bool IsObjectFocused(this TreeView treeView, object obj) public static void ExpandObject(this TreeView treeView, object obj) public static void ExpandObject(this TreeView treeView, object obj, bool expanded) public static bool IsObjectExpanded(this TreeView treeView, object obj) public static TreeViewItem GetParentItem(this TreeViewItem item)
文章地址如下:WPF TreeView tools
但是,这里面有一个小bug:当TreeView节点中使用延迟绑定的时候,根据数据节点获取TreeItem会失败。这里我把它修正了一下,感兴趣的朋友可以直接使用我修改后的函数。
public static class TreeViewTools { /// <summary> /// Returns the TreeViewItem of a data bound object. /// </summary> /// <param name="treeView">TreeView</param> /// <param name="obj">Data bound object</param> /// <returns>The TreeViewItem of the data bound object or null.</returns> public static TreeViewItem GetItemFromObject(this TreeView treeView, object obj) { try { DependencyObject dObject = GetContainerFormObject(treeView, obj); TreeViewItem tvi = dObject as TreeViewItem; while (tvi == null) { dObject = VisualTreeHelper.GetParent(dObject); tvi = dObject as TreeViewItem; } return tvi; } catch { } return null; } private static DependencyObject GetContainerFormObject(ItemsControl item, object obj) { if (item == null) return null; DependencyObject dObject = null; dObject = item.ItemContainerGenerator.ContainerFromItem(obj); if (dObject != null) return dObject; var query = from childItem in item.Items.Cast<object>() let childControl = item.ItemContainerGenerator.ContainerFromItem(childItem) as ItemsControl select GetContainerFormObject(childControl, obj); return query.FirstOrDefault(i => i != null); } /// <summary> /// Selects a data bound object of a TreeView. /// </summary> /// <param name="treeView">TreeView</param> /// <param name="obj">Data bound object</param> public static void SelectObject(this TreeView treeView, object obj) { treeView.SelectObject(obj, true); } /// <summary> /// Selects or deselects a data bound object of a TreeView. /// </summary> /// <param name="treeView">TreeView</param> /// <param name="obj">Data bound object</param> /// <param name="selected">select or deselect</param> public static void SelectObject(this TreeView treeView, object obj, bool selected) { var tvi = treeView.GetItemFromObject(obj); if (tvi != null) { tvi.IsSelected = selected; } } /// <summary> /// Returns if a data bound object of a TreeView is selected. /// </summary> /// <param name="treeView">TreeView</param> /// <param name="obj">Data bound object</param> /// <returns>Returns true if the object is selected, and false if it is not selected or obj is not in the tree.</returns> public static bool IsObjectSelected(this TreeView treeView, object obj) { var tvi = treeView.GetItemFromObject(obj); if (tvi != null) { return tvi.IsSelected; } return false; } /// <summary> /// Returns if a data bound object of a TreeView is focused. /// </summary> /// <param name="treeView">TreeView</param> /// <param name="obj">Data bound object</param> /// <returns>Returns true if the object is focused, and false if it is not focused or obj is not in the tree.</returns> public static bool IsObjectFocused(this TreeView treeView, object obj) { var tvi = treeView.GetItemFromObject(obj); if (tvi != null) { return tvi.IsFocused; } return false; } /// <summary> /// Expands a data bound object of a TreeView. /// </summary> /// <param name="treeView">TreeView</param> /// <param name="obj">Data bound object</param> public static void ExpandObject(this TreeView treeView, object obj) { treeView.ExpandObject(obj, true); } /// <summary> /// Expands or collapses a data bound object of a TreeView. /// </summary> /// <param name="treeView">TreeView</param> /// <param name="obj">Data bound object</param> /// <param name="expanded">expand or collapse</param> public static void ExpandObject(this TreeView treeView, object obj, bool expanded) { var tvi = treeView.GetItemFromObject(obj); if (tvi != null) { tvi.IsExpanded = expanded; if (expanded) { // update layout, so that following calls to f.e. SelectObject on child nodes will // find theire TreeViewNodes treeView.UpdateLayout(); } } } /// <summary> /// Returns if a douta bound object of a TreeView is expanded. /// </summary> /// <param name="treeView">TreeView</param> /// <param name="obj">Data bound object</param> /// <returns>Returns true if the object is expanded, and false if it is collapsed or obj is not in the tree.</returns> public static bool IsObjectExpanded(this TreeView treeView, object obj) { var tvi = treeView.GetItemFromObject(obj); if (tvi != null) { return tvi.IsExpanded; } return false; } /// <summary> /// Retuns the parent TreeViewItem. /// </summary> /// <param name="item">TreeViewItem</param> /// <returns>Parent TreeViewItem</returns> public static TreeViewItem GetParentItem(this TreeViewItem item) { var dObject = VisualTreeHelper.GetParent(item); TreeViewItem tvi = dObject as TreeViewItem; while (tvi == null) { dObject = VisualTreeHelper.GetParent(dObject); tvi = dObject as TreeViewItem; } return tvi; } }
加载全部内容