WPF中TreeView控件的用法
在WPF的TreeView使用方式和WinForm下有很大不同,那些展開(kāi)某節(jié)點(diǎn)、獲取父節(jié)點(diǎn),判斷某節(jié)點(diǎn)是否被選中等常用的操作在WinForm下都有相關(guān)函數(shù),而在WPF中卻不能輕易實(shí)現(xiàn)。
一種常規(guī)的方式是通過(guò)MVVM模式來(lái)將TreeViewItem節(jié)點(diǎn)中的IsSelect,IsExpanded等屬性來(lái)雙向綁定到要顯示的節(jié)點(diǎn)數(shù)據(jù)中,然后直接通過(guò)節(jié)點(diǎn)數(shù)據(jù)的屬性來(lái)實(shí)現(xiàn)相關(guān)操作。
但是,有的時(shí)候,當(dāng)我們沒(méi)有ViewModel層,但又想像WinFrom那樣直接簡(jiǎn)單的獲取或設(shè)置這些屬性的時(shí)候,該如何辦呢。其實(shí)WPF還是提供了類(lèi)似WinForm中的這些設(shè)置的,只不過(guò)形式不一樣了而已,但是卻沒(méi)WinFrom的那么直觀和方便。CodeProject上就有人將常用函數(shù)總結(jié)了一下,寫(xiě)成了擴(kuò)展函數(shù),主要提供如下功能:
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
但是,這里面有一個(gè)小bug:當(dāng)TreeView節(jié)點(diǎn)中使用延遲綁定的時(shí)候,根據(jù)數(shù)據(jù)節(jié)點(diǎn)獲取TreeItem會(huì)失敗。這里我把它修正了一下,感興趣的朋友可以直接使用我修改后的函數(shù)。
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; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c#委托把方法當(dāng)成參數(shù)(實(shí)例講解)
本篇文章主要是對(duì)c#委托把方法當(dāng)成參數(shù)的實(shí)例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01winform天氣預(yù)報(bào)小工具(附源碼下載)
主要原理就是利用網(wǎng)上免費(fèi)的webservice獲取天氣數(shù)據(jù),需要的朋友可以參考下2012-03-03C#編寫(xiě)游戲客戶端的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#編寫(xiě)游戲客戶端的實(shí)現(xiàn)代碼,連接客戶端原理流程圖,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11在C#中g(shù)lobal關(guān)鍵字的作用及其用法
global 是 C# 2.0 中新增的關(guān)鍵字,理論上說(shuō),如果代碼寫(xiě)得好的話,根本不需要用到它,但是不排除一些特別的情況,比如修改別人的代碼,本文僅舉例說(shuō)明。2016-03-03利用C#/VB.NET實(shí)現(xiàn)將PDF轉(zhuǎn)為Word
眾所周知,PDF 文檔支持特長(zhǎng)文件,集成度和安全可靠性都較高,可有效防止他人對(duì) PDF 內(nèi)容進(jìn)行更改,所以在工作中深受大家喜愛(ài)。本文將分為兩部分介紹如何以編程的方式將 PDF 轉(zhuǎn)換為 Word,需要的可以參考一下2022-12-12C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問(wèn)題
這篇文章主要介紹了C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01