WPF TreeView IsExpanded 綁定不上的問題

来源:https://www.cnblogs.com/Johar/archive/2018/10/31/9886646.html
-Advertisement-
Play Games

最近項目上需要通過MVVM來控制TreeView,其中需要需要控制通過搜索來定位某個節點,正常邏輯下,首先通過需要在樹上面找到該節點,然後選中該節點,並將該節點的父節點展開,這個時候需要通過MVVM來控制,需要綁定起來,只是一直沒有binding上,代碼如下: MVVM示例代碼: 界面代碼: 數據的 ...


最近項目上需要通過MVVM來控制TreeView,其中需要需要控制通過搜索來定位某個節點,正常邏輯下,首先通過需要在樹上面找到該節點,然後選中該節點,並將該節點的父節點展開,這個時候需要通過MVVM來控制,需要綁定起來,只是一直沒有binding上,代碼如下:

MVVM示例代碼:

  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 
  8 namespace DragDrop
  9 {
 10     class DataItem : NotifyPropertyBase, ICloneable
 11     {
 12         public DataItem(string header, int deepth = 1)
 13         {
 14             Header = header;
 15             Deepth = deepth;
 16         }
 17 
 18         public object Clone()
 19         {
 20             DataItem dataItem = new DataItem(Header, Deepth);
 21             dataItem.IsExpanded = IsExpanded;
 22             dataItem.IsSelected = IsSelected;
 23             dataItem.Deepth = Deepth;
 24             foreach (DataItem item in Items)
 25                 dataItem.Items.Add((DataItem)item.Clone());
 26             return dataItem;
 27         }
 28 
 29         private string header;
 30         public string Header
 31         {
 32             get { return header; }
 33             set
 34             {
 35                 SetProperty(ref header, value);
 36             }
 37         }
 38 
 39         private bool isSelected;
 40         public bool IsSelected
 41         {
 42             get { return isSelected; }
 43             set { SetProperty(ref isSelected, value); }
 44         }
 45 
 46         private bool isExpanded;
 47         public bool IsExpanded
 48         {
 49             get { return isExpanded; }
 50             set
 51             {
 52                 SetProperty(ref isExpanded, value);
 53                 Console.WriteLine("{0}--{1}", Header, IsExpanded);
 54             }
 55         }
 56 
 57         private int deepth;
 58         public int Deepth
 59         {
 60             get { return deepth; }
 61             set
 62             {
 63                 if (deepth != value)
 64                 {
 65                     deepth = value;
 66                     SetProperty(ref deepth, value);
 67                 }
 68             }
 69         }
 70 
 71         private ObservableCollection<DataItem> mItems = null;
 72         public ObservableCollection<DataItem> Items
 73         {
 74             get
 75             {
 76                 if (mItems == null)
 77                     mItems = new ObservableCollection<DataItem>();
 78                 return mItems;
 79             }
 80         }       
 81         
 82     }
 83 
 84     class Data
 85     {
 86         private static Data mInstance = new Data();
 87 
 88         public static Data Instance
 89         {
 90             get { return mInstance; }
 91         }
 92 
 93         private ObservableCollection<DataItem> GenerateTreeViewItems()
 94         {
 95             ObservableCollection<DataItem> items = new ObservableCollection<DataItem>();
 96 
 97             DataItem item1 = new DataItem("TreeViewItem1");
 98             item1.Items.Add(new DataItem("SubItem1", item1.Deepth));
 99             item1.Items.Add(new DataItem("SubItem2", item1.Deepth));
100             item1.Items.Add(new DataItem("SubItem3", item1.Deepth));
101             item1.Items.Add(new DataItem("SubItem4", item1.Deepth));
102             items.Add(item1);
103 
104             DataItem item2 = new DataItem("TreeViewItem2");
105             item2.Items.Add(new DataItem("SubItem1", item2.Deepth));
106             item2.Items.Add(new DataItem("SubItem2", item2.Deepth));
107             items.Add(item2);
108 
109             DataItem item3 = new DataItem("TreeViewItem3");
110             item3.Items.Add(new DataItem("SubItem1", item3.Deepth));
111             item3.Items.Add(new DataItem("SubItem2", item3.Deepth));
112             item3.Items.Add(new DataItem("SubItem3", item3.Deepth));
113             item3.Items.Add(new DataItem("SubItem4", item3.Deepth));
114             item3.Items.Add(new DataItem("SubItem5", item3.Deepth));
115             item3.Items.Add(new DataItem("SubItem6", item3.Deepth));
116             item3.Items.Add(new DataItem("SubItem7", item3.Deepth));
117             item3.Items.Add(new DataItem("SubItem8", item3.Deepth));
118             items.Add(item3);
119 
120             return items;
121         }
122 
123         private ObservableCollection<DataItem> GenerateListItems()
124         {
125             ObservableCollection<DataItem> items = new ObservableCollection<DataItem>();
126             items.Add(new DataItem("ListBoxItem1"));
127             items.Add(new DataItem("ListBoxItem2"));
128             items.Add(new DataItem("ListBoxItem3"));
129             items.Add(new DataItem("ListBoxItem4"));
130             items.Add(new DataItem("ListBoxItem5"));
131             return items;
132         }
133 
134         public ObservableCollection<DataItem> TreeViewItems
135         {
136             get
137             {
138                 if (mTreeViewItems == null)
139                     mTreeViewItems = GenerateTreeViewItems();
140                 return mTreeViewItems;
141             }
142         }
143 
144         public ObservableCollection<DataItem> ListBoxItems
145         {
146             get
147             {
148                 if (mListBoxItems == null)
149                     mListBoxItems = GenerateListItems();
150                 return mListBoxItems;
151             }
152         }
153 
154         private ObservableCollection<DataItem> mTreeViewItems = null;
155         private ObservableCollection<DataItem> mListBoxItems = null;
156     }
157 }

界面代碼:

 1 <Window x:Class="DragDrop.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:DragDrop"
 7         mc:Ignorable="d"
 8         Title="MainWindow" Height="350" Width="525">
 9     <Window.Resources>
10         <HierarchicalDataTemplate x:Key="treeViewTemplate" DataType="{x:Type local:DataItem}" ItemsSource="{Binding Items}">
11             <TextBlock Text="{Binding Header}"/>
12         </HierarchicalDataTemplate>
13 
14         <Style x:Key="TreeViewStyle" TargetType="{x:Type TreeViewItem}">
15             <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
16             <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
17             <Style.Triggers>
18                 <DataTrigger Binding="{Binding Deepth}" Value="1">
19                     <Setter Property="IsExpanded" Value="True"/>
20                 </DataTrigger>
21             </Style.Triggers>
22         </Style>
23     </Window.Resources>
24     <Grid x:Name="mTopLevelGrid">
25         <TreeView x:Name="mTreeView" Grid.Column="0"
26                   ItemsSource="{Binding Source={x:Static local:Data.Instance}, Path=TreeViewItems}"
27                   ItemTemplate="{StaticResource treeViewTemplate}"
28                   ItemContainerStyle="{StaticResource TreeViewStyle}"/>
29     </Grid>
30 </Window>

數據的綁定沒有問題,界面沒有問題,奇怪的是IsSelected可以正常綁定,但是IsExpanded就是不行,後來發現這兩個屬性唯一的區別就是在TreeView的式樣中

1 <Style.Triggers>
2      <DataTrigger Binding="{Binding Deepth}" Value="1">
3            <Setter Property="IsExpanded" Value="True"/>
4       </DataTrigger>
5 </Style.Triggers>

後面把這個DataTrigger這段代碼註釋掉,編譯一下,重新運行,IsExpanded就可以正常綁定。後面自己思考了一下,MVVM模式實現將ViewModel與界面上面的顯示綁定起來,實際上也就是使用了觀察者模式,而Trigger的原理應該和數據binding是一樣的,而且這個Trigger寫在後面,可能直接把前面的綁定取代了,但是試了一下,將DataTrigger放到前面,也還是綁定不上,這種可能性排除;個人猜測可能是Trigger的優先順序比較高,從而造成IsExpanded一直Binding不上去。


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

-Advertisement-
Play Games
更多相關文章
  • 創建Django項目: 命令行創建:python manage.py startproject 項目名 啟動Django項目: 根目錄下(有manage.py的目錄) python manage.py runserver IP:埠(可直接寫埠或預設在本地的8000埠下) 創建APP: 命令行創 ...
  • netty 與 webSocket 起因 有個需求需要用到 ,然後最近又正好在學 ,然後合起來走一波。寫篇文章記錄一下,做一個念想。 協議格式 開始 我們先寫一個什麼都不加的 熱熱手,話不多說,代碼如下 常規的netty入門示例,加了個String的編碼和解碼器,還加了一個列印消息的 ,並不是什麼太 ...
  • 1. 文件操作 open() 文件句柄 open()打開一個文件, 獲取的是文件句柄 read() #讀取全部內容 read(n)#讀取前n個字元 readline()#讀取一行 且讀取出來末尾都有\n readlines()#讀取全部 每一⾏形成一個元素並放到列表 註意: 讀取完的文件句柄一定要關 ...
  • package zrs; public class javaDay02_3 { public static void main(String[] args){ //switch 結構 int x=5; switch(x){//x 支持byte short int char 5.0以後 enum St ...
  • ·字元串(string) @ title @ title @ 小結: ·字元串(string) @ title @ title @ 小結: ****** 幾米花的Python ****** 博客主頁:https://www.cnblogs.com/jimmy-share/ 歡迎轉載 ~ ...
  • [TOC] 1. 函數名的使用 其實函數名也是一個變數,但它是一個比較特殊的變數,與小括弧配合可以執行函數的變數: 函數名其實和記憶體一樣,也可以使用 查看它的記憶體地址: 函數名賦值給其他變數 函數也能當作容器類的元素: 函數名也能當作函數的參數: 函數名也可以作為函數的返回值: 2. 閉包 閉包是指 ...
  • 界面如下圖: 動圖效果演示: ...
  • 加群731738614加群731738614 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...