Seaching TreeVIew WPF

来源:https://www.cnblogs.com/Johar/archive/2018/11/28/10028962.html
-Advertisement-
Play Games

項目中有一個樹形結構的資源,需要支持搜索功能,搜索出來的結果還是需要按照樹形結構展示,下麵是簡單實現的demo。 1.首先創建TreeViewItem的ViewModel,一般情況下,樹形結構都包含DisplayName,Deepth,Parent,Children,Id, IndexCode,Vi ...


項目中有一個樹形結構的資源,需要支持搜索功能,搜索出來的結果還是需要按照樹形結構展示,下麵是簡單實現的demo。

1.首先創建TreeViewItem的ViewModel,一般情況下,樹形結構都包含DisplayName,Deepth,Parent,Children,Id, IndexCode,Visibility等屬性,具體代碼如下所示:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Collections.ObjectModel;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 using System.Windows;
  8 
  9 namespace TreeViewDemo
 10 {
 11     public class TreeViewItemVM : NotifyPropertyChangedBase
 12     {
 13         public TreeViewItemVM ()
 14         {
 15             Visible = Visibility.Visible;
 16         }
 17 
 18         private TreeViewItemVM parent;
 19         public TreeViewItemVM Parent
 20         {
 21             get
 22             {
 23                 return this.parent;
 24             }
 25             set
 26             {
 27                 if (this.parent != value)
 28                 {
 29                     this.parent = value;
 30                     this.OnPropertyChanged(() => this.Parent);
 31                 }
 32             }
 33         }
 34 
 35         private ObservableCollection<TreeViewItemVM> children;
 36         public ObservableCollection<TreeViewItemVM> Children
 37         {
 38             get
 39             {
 40                 return this.children;
 41             }
 42             set
 43             {
 44                 if (this.children != value)
 45                 {
 46                     this.children = value;
 47                     this.OnPropertyChanged(() => this.Children);
 48                 }
 49             }
 50         }
 51 
 52         private string id;
 53         public string ID
 54         {
 55             get
 56             {
 57                 return this.id;
 58             }
 59             set
 60             {
 61                 if (this.id != value)
 62                 {
 63                     this.id = value;
 64                     this.OnPropertyChanged(() => this.ID);
 65                 }
 66             }
 67         }
 68 
 69         private string indexCode;
 70         public string IndexCode
 71         {
 72             get { return indexCode; }
 73             set
 74             {
 75                 if (indexCode != value)
 76                 {
 77                     indexCode = value;
 78                     this.OnPropertyChanged(() => IndexCode);
 79                 }
 80             }
 81         }
 82 
 83         private string displayName;
 84         public string DisplayName
 85         {
 86             get
 87             {
 88                 return this.displayName;
 89             }
 90             set
 91             {
 92                 if (this.displayName != value)
 93                 {
 94                     this.displayName = value;
 95                     this.OnPropertyChanged(() => this.DisplayName);
 96                 }
 97             }
 98         }
 99 
100         private int deepth;
101         public int Deepth
102         {
103             get
104             {
105                 return this.deepth;
106             }
107             set
108             {
109                 if (this.deepth != value)
110                 {
111                     this.deepth = value;
112                     this.OnPropertyChanged(() => this.Deepth);
113                 }
114             }
115         }
116 
117         private bool hasChildren;
118         public bool HasChildren
119         {
120             get
121             {
122                 return this.hasChildren;
123             }
124             set
125             {
126                 if (this.hasChildren != value)
127                 {
128                     this.hasChildren = value;
129                     this.OnPropertyChanged(() => this.HasChildren);
130                 }
131             }
132         }
133 
134         private NodeType type;
135         public NodeType Type
136         {
137             get { return type; }
138             set
139             {
140                 if (type != value)
141                 {
142                     type = value;
143                     OnPropertyChanged(() => this.Type);
144                 }
145             }
146         }
147 
148         private Visibility visible;
149         public Visibility Visible
150         {
151             get { return visible; }
152             set
153             {
154                 if (visible != value)
155                 {
156                     visible = value;
157                     OnPropertyChanged(() => this.Visible);
158                 }
159             }
160         }
161 
162         public bool NameContains(string filter)
163         {
164             if (string.IsNullOrWhiteSpace(filter))
165             {
166                 return true;
167             }
168 
169             return DisplayName.ToLowerInvariant().Contains(filter.ToLowerInvariant());
170         }
171     }
172 }

2.創建TreeViewViewModel,其中定義了用於過濾的屬性Filter,以及過濾函數,併在構造函數中初始化一些測試數據,具體代碼如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Collections.ObjectModel;
  4 using System.ComponentModel;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 using System.Windows.Data;
  9 
 10 namespace TreeViewDemo
 11 {
 12     public class TreeViewViewModel : NotifyPropertyChangedBase
 13     {
 14         public static TreeViewViewModel Instance = new TreeViewViewModel();
 15 
 16         private TreeViewViewModel()
 17         {
 18             Filter = string.Empty;
 19 
 20             Root = new TreeViewItemVM()
 21             {
 22                 Deepth = 0,
 23                 DisplayName = "五號線",
 24                 HasChildren = true,
 25                 Type = NodeType.Unit,
 26                 ID = "0",
 27                 Children = new ObservableCollection<TreeViewItemVM>() { 
 28                     new TreeViewItemVM() { DisplayName = "站臺", Deepth = 1, HasChildren = true, ID = "1", Type = NodeType.Region,
 29                         Children = new ObservableCollection<TreeViewItemVM>(){
 30                             new TreeViewItemVM() { DisplayName = "Camera 01", Deepth = 2, HasChildren = false, ID = "3",Type = NodeType.Camera },
 31                             new TreeViewItemVM() { DisplayName = "Camera 02", Deepth = 2, HasChildren = false, ID = "4",Type = NodeType.Camera },
 32                             new TreeViewItemVM() { DisplayName = "Camera 03", Deepth = 2, HasChildren = false, ID = "5",Type = NodeType.Camera },
 33                             new TreeViewItemVM() { DisplayName = "Camera 04", Deepth = 2, HasChildren = false, ID = "6",Type = NodeType.Camera },
 34                             new TreeViewItemVM() { DisplayName = "Camera 05", Deepth = 2, HasChildren = false, ID = "7", Type = NodeType.Camera},
 35                         }},
 36                     new TreeViewItemVM() { DisplayName = "進出口", Deepth = 1, HasChildren = true, ID = "10", Type = NodeType.Region,
 37                         Children = new ObservableCollection<TreeViewItemVM>(){
 38                             new TreeViewItemVM() { DisplayName = "Camera 11", Deepth = 2, HasChildren = false, ID = "13",Type = NodeType.Camera },
 39                             new TreeViewItemVM() { DisplayName = "Camera 12", Deepth = 2, HasChildren = false, ID = "14",Type = NodeType.Camera },
 40                             new TreeViewItemVM() { DisplayName = "Camera 13", Deepth = 2, HasChildren = false, ID = "15",Type = NodeType.Camera },
 41                             new TreeViewItemVM() { DisplayName = "Camera 14", Deepth = 2, HasChildren = false, ID = "16", Type = NodeType.Camera},
 42                             new TreeViewItemVM() { DisplayName = "Camera 15", Deepth = 2, HasChildren = false, ID = "17", Type = NodeType.Camera},
 43                         }},
 44                 }
 45             };
 46 
 47             InitTreeView();
 48         }
 49 
 50         private ObservableCollection<TreeViewItemVM> selectedCameras = new ObservableCollection<TreeViewItemVM>();
 51 
 52         private TreeViewItemVM root;
 53         public TreeViewItemVM Root
 54         {
 55             get
 56             {
 57                 return this.root;
 58             }
 59             set
 60             {
 61                 if (this.root != value)
 62                 {
 63                     this.root = value;
 64                     this.OnPropertyChanged(() => this.Root);
 65                 }
 66             }
 67         }
 68 
 69         /// <summary>
 70         /// 過濾欄位
 71         /// </summary>
 72         private string filter;
 73         public string Filter
 74         {
 75             get
 76             {
 77                 return this.filter;
 78             }
 79             set
 80             {
 81                 if (this.filter != value)
 82                 {
 83 
 84                     this.filter = value;
 85                     this.OnPropertyChanged(() => this.Filter);
 86 
 87                     this.Refresh();
 88                 }
 89             }
 90         }
 91 
 92         /// <summary>
 93         /// View
 94         /// </summary>
 95         protected ICollectionView view;
 96         public ICollectionView View
 97         {
 98             get
 99             {
100                 return this.view;
101             }
102             set
103             {
104                 if (this.view != value)
105                 {
106                     this.view = value;
107                     this.OnPropertyChanged(() => this.View);
108                 }
109             }
110         }
111 
112         /// <summary>
113         /// 刷新View
114         /// </summary>
115         public void Refresh()
116         {
117             if (this.View != null)
118             {
119                 this.View.Refresh();
120             }
121         }
122 
123         private bool DoFilter(Object obj)
124         {
125             TreeViewItemVM item = obj as TreeViewItemVM;
126             if (item == null)
127             {
128                 return true;
129             }
130 
131             bool result = false;
132             foreach (var node in item.Children)
133             {
134                 result = TreeItemDoFilter(node) || result;
135             }
136 
137             return result || item.NameContains(this.Filter);
138         }
139 
140         private bool TreeItemDoFilter(TreeViewItemVM vm)
141         {
142             if (vm == null)
143             {
144                 return true;
145             }
146 
147             bool result = false;
148             if (vm.Type == NodeType.Region || vm.Type == NodeType.Unit)
149             {
150                 foreach (var item in vm.Children)
151                 {
152                     result = TreeItemDoFilter(item) || result;
153                 }
154             }
155 
156             if (result || vm.NameContains(this.Filter))
157             {
158                 result = true;
159                 vm.Visible = System.Windows.Visibility.Visible;
160             }
161             else
162             {
163                 vm.Visible = System.Windows.Visibility.Collapsed;
164             }
165 
166             return result;
167         }
168 
169         public void InitTreeView()
170         {
171             this.View = CollectionViewSource.GetDefaultView(this.Root.Children);
172             this.View.Filter = this.DoFilter;
173             this.Refresh();
174         }
175     }
176 }

3.在界面添加一個TreeView,並添加一個簡單的Style,將ViewModel中必要數據進行綁定:

 1 <Window x:Class="TreeViewDemo.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="450" Width="525">
 5     <Window.Resources>
 6         <Style x:Key="style" TargetType="{x:Type TreeViewItem}">
 7             <Setter Property="Template">
 8                 <Setter.Value>
 9                     <ControlTemplate TargetType="{x:Type TreeViewItem}">
10                         <Grid Visibility="{Binding Visible}" Background="{Binding Background}">
11                             <ContentPresenter ContentSource="Header"/>
12                         </Grid>
13                         
14                         <ControlTemplate.Triggers>
15                             <Trigger Property="IsSelected" Value="true">
16                                 <Setter Property="Background" Value="Green"/>
17                             </Trigger>
18                         </ControlTemplate.Triggers>
19                     </ControlTemplate>
20                 </Setter.Value>
21             </Setter>
22         </Style>
23     </Window.Resources>
24     <Grid>
25         <Grid.RowDefinitions>
26             <RowDefinition Height="Auto"/>
27             <RowDefinition Height="*"/>
28         </Grid.RowDefinitions>
29 
30         <TextBox x:Name="searchTxt" Width="200" HorizontalAlignment="Center" Height="40"
31                  Margin="20" Text="{Binding Filter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
32 
33         <TreeView
34                   Grid.Row="1"
35                   ItemsSource="{Binding View}">
36             <TreeView.ItemTemplate>
37                 <HierarchicalDataTemplate ItemContainerStyle ="{StaticResource style}"  ItemsSource="{Binding Children}">
38                     <Grid Height="25" >
39                         <TextBlock
40                             x:Name="txt"
41                             VerticalAlignment="Center"
42                             Text="{Binding DisplayName}"    
43                             TextTrimming="CharacterEllipsis"
44                             ToolTip="{Binding DisplayName}" />
45                     </Grid>
46                 </HierarchicalDataTemplate>
47             </TreeView.ItemTemplate>
48         </TreeView>
49     </Grid>
50 </Window>

4.在給界面綁定具體的數據

 1 using System.Windows;
 2 
 3 namespace TreeViewDemo
 4 {
 5     /// <summary>
 6     /// MainWindow.xaml 的交互邏輯
 7     /// </summary>
 8     public partial class MainWindow : Window
 9     {
10         public MainWindow()
11         {
12             InitializeComponent();
13             this.Loaded += MainWindow_Loaded;
14         }
15 
16         void MainWindow_Loaded(object sender, RoutedEventArgs e)
17         {
18             this.DataContext = TreeViewViewModel.Instance;
19         }
20     }
21 }

5.運行結果:

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近在學習leal的時候遇到了一點非常迷惑的地方,就是leal是用來取有效地址的,但是為什麼它也可以實現賦值呢?偶然發現一個博客講的不錯,遂自己記錄一下 一個這樣的例子 如果寄存器edx裡面存的值為x,我們知道這句結束之後edx裡面的值會被置為5x+7,但是看起來為什麼是值而不是地址呢? 實際上這之 ...
  • ​ 類型是一個創建或獲取服務實例的類型,這個類型繼承了 這個類型,也是使用了訪問者模式,下麵一一來解析此類 ServiceProviderEngineScope 在解析 之前先看一下 類型,這個類型就可以是一個容器類型,最後實例化的服務對象就緩存在此類之中, 從下麵代碼中可以看出此類實現了 和`IS ...
  • 這一系列文章的內容是從MSDN中COPY過來的,講述的是最簡單的WCF程式示例:如何在控制台應用程式實現和承載WCF服務,以及如何創建、配置和使用WCF客戶端。 文章主體可分為兩部分,分別介紹伺服器端和客戶端的編程實現。細分的話,可以分為六項任務。 伺服器端 定義WCF服務協定(任務一) 這是創建基 ...
  • ABP提供了在啟動時配置模塊的基礎設施和模型。 1.配置ABP 配置ABP是在模塊的PreInitialize方法中完成的,例如: public class SimpleTaskSystemModule : AbpModule { public override void PreInitialize ...
  • ASP.NET Core MVC 提供了基於角色( Role )、聲明( Chaim ) 和策略 ( Policy ) 等的授權方式。在實際應用中,可能採用部門( Department , 本文采用用戶組 Group )、職位 ( 可繼續沿用 Role )、許可權( Permission )的方式進行... ...
  • 寫在前面 上面文章我給大家介紹了Dapper這個ORM框架的簡單使用,大伙會用了嘛!本來今天這篇文章是要講Vue的快速入門的,原因是想在後面的文章中使用Vue進行這個CMS系統的後臺管理界面的實現。但是奈何Vue實現的SPA有一定的門檻,不太適合新手朋友,所以為了照顧大多數人,我準備還是採用asp. ...
  • 一、前言 最近一兩個星期,加班,然後回去後弄自己的博客,把自己的電腦從 Windows 10 改到 Ubuntu 18.10 又弄回 Windows 10,原本計劃的學習 Vue 中生命周期的相關知識目前也沒有任何的進展,嗯,罪過罪過。看了眼時間,11月也快要結束了,準備補上一篇如何將我們的 .NE ...
  • const是一個c#語言的關鍵字,它限定一個變數不允許被改變 const一般修飾的變數為只讀變數 const只能在初期就使用常量初始化好,而且對也每一次編譯後的結果,const的值都是固定的 使用const在一定程度上可以提高程式的安全性和可靠性 再次賦值報錯 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...