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#模擬http 發(fā)送post或get請(qǐng)求的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇C#模擬http 發(fā)送post或get請(qǐng)求的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
HttpWebRequest實(shí)現(xiàn)下載圖片至本地
這篇文章主要為大家詳細(xì)介紹了HttpWebRequest實(shí)現(xiàn)下載圖片至本地,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
C# Ado.net實(shí)現(xiàn)讀取SQLServer數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程列表及參數(shù)信息示例
這篇文章主要介紹了C# Ado.net實(shí)現(xiàn)讀取SQLServer數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程列表及參數(shù)信息,結(jié)合實(shí)例形式總結(jié)分析了C#針對(duì)SQLServer數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程及參數(shù)信息的各種常見(jiàn)操作技巧,需要的朋友可以參考下2019-02-02
C# 使用匿名函數(shù)解決EventHandler參數(shù)傳遞的難題
C#動(dòng)態(tài)生成PictureBox并綁定右鍵菜單,實(shí)現(xiàn)刪除圖片2009-05-05
C#讀取word中表格數(shù)據(jù)的方法實(shí)現(xiàn)
本文主要介紹了C#讀取word中表格數(shù)據(jù)的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

