欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF中TreeView控件的用法

 更新時間:2022年06月17日 10:25:27   作者:天方  
這篇文章介紹了WPF中TreeView控件的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

在WPF的TreeView使用方式和WinForm下有很大不同,那些展開某節(jié)點、獲取父節(jié)點,判斷某節(jié)點是否被選中等常用的操作在WinForm下都有相關(guān)函數(shù),而在WPF中卻不能輕易實現(xiàn)。

一種常規(guī)的方式是通過MVVM模式來將TreeViewItem節(jié)點中的IsSelect,IsExpanded等屬性來雙向綁定到要顯示的節(jié)點數(shù)據(jù)中,然后直接通過節(jié)點數(shù)據(jù)的屬性來實現(xiàn)相關(guān)操作。

但是,有的時候,當我們沒有ViewModel層,但又想像WinFrom那樣直接簡單的獲取或設置這些屬性的時候,該如何辦呢。其實WPF還是提供了類似WinForm中的這些設置的,只不過形式不一樣了而已,但是卻沒WinFrom的那么直觀和方便。CodeProject上就有人將常用函數(shù)總結(jié)了一下,寫成了擴展函數(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

但是,這里面有一個小bug:當TreeView節(jié)點中使用延遲綁定的時候,根據(jù)數(shù)據(jù)節(jié)點獲取TreeItem會失敗。這里我把它修正了一下,感興趣的朋友可以直接使用我修改后的函數(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;
        }
    }

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#中is與As運算符號的使用詳解

    C#中is與As運算符號的使用詳解

    本篇文章是對C#中is與As運算符號的使用進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • c#委托把方法當成參數(shù)(實例講解)

    c#委托把方法當成參數(shù)(實例講解)

    本篇文章主要是對c#委托把方法當成參數(shù)的實例代碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • winform天氣預報小工具(附源碼下載)

    winform天氣預報小工具(附源碼下載)

    主要原理就是利用網(wǎng)上免費的webservice獲取天氣數(shù)據(jù),需要的朋友可以參考下
    2012-03-03
  • C#編寫游戲客戶端的實現(xiàn)代碼

    C#編寫游戲客戶端的實現(xiàn)代碼

    這篇文章主要介紹了C#編寫游戲客戶端的實現(xiàn)代碼,連接客戶端原理流程圖,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-11-11
  • 在C#中g(shù)lobal關(guān)鍵字的作用及其用法

    在C#中g(shù)lobal關(guān)鍵字的作用及其用法

    global 是 C# 2.0 中新增的關(guān)鍵字,理論上說,如果代碼寫得好的話,根本不需要用到它,但是不排除一些特別的情況,比如修改別人的代碼,本文僅舉例說明。
    2016-03-03
  • 利用C#/VB.NET實現(xiàn)將PDF轉(zhuǎn)為Word

    利用C#/VB.NET實現(xiàn)將PDF轉(zhuǎn)為Word

    眾所周知,PDF 文檔支持特長文件,集成度和安全可靠性都較高,可有效防止他人對 PDF 內(nèi)容進行更改,所以在工作中深受大家喜愛。本文將分為兩部分介紹如何以編程的方式將 PDF 轉(zhuǎn)換為 Word,需要的可以參考一下
    2022-12-12
  • C#簡單實現(xiàn)文件上傳功能

    C#簡單實現(xiàn)文件上傳功能

    這篇文章主要介紹了C#簡單實現(xiàn)文件上傳功能,利用MVC+EF+LigerUI 實現(xiàn)的upload上傳功能,感興趣的小伙伴們可以參考一下
    2016-03-03
  • C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問題

    C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問題

    這篇文章主要介紹了C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • C#執(zhí)行DOS命令的方法

    C#執(zhí)行DOS命令的方法

    這篇文章主要介紹了C#執(zhí)行DOS命令的方法,涉及針對進程的調(diào)用以及系統(tǒng)DOS命令的使用,具有不錯的實用價值,需要的朋友可以參考下
    2014-11-11
  • C#中split用法實例總結(jié)

    C#中split用法實例總結(jié)

    這篇文章主要介紹了C#中split用法,結(jié)合實例形式總結(jié)分析了C#常見的split操作字符串相關(guān)技巧,需要的朋友可以參考下
    2016-06-06

最新評論