WPF裡ItemsControl的分組實現

2021-09-25 23:40:07 字數 1404 閱讀 1320

我們在用到itemscontrol時,有時會用到分組,如listbox,listview,datagrid。wpf的itemscontrol可以實現分組,是依託於groupstyle,以listbox為例,他的分組效果圖為:

以下為前台:

1 2     3         4             5                 7                 9                 11             

12

13

14 15 16 1740

41

42

43

從16行可以看出,groupstyle定義的是控制項內部樣式,所以有人嘗試在這裡綁實體資料屬性的話肯定是失敗的,注意25行只能是name,不管分組的屬性叫什麼名,這都只能是name,我寫了個button在裡面,如果想知道為什麼只能是name,寫個click處理,把button的datacontext列印出來就什麼都知道了。如果想在這裡做更多的處理,比如進行一些負責的運算,可以寫加轉換器。

這裡只是弄了乙個原始的expander裝載分組控制項,需要美化可以另寫樣式。

以下是後台:

1 public class modelfile

2 4 public string authorname

5 public string uptime

6 }7 8 public partial class mainwindow : window

9 );

17 collectionmodelfile.add(new modelfile() );

18 collectionmodelfile.add(new modelfile() );

19 collectionmodelfile.add(new modelfile() );

20 collectionmodelfile.add(new modelfile() );

21 collectionmodelfile.add(new modelfile() );

22 collectionmodelfile.add(new propertygroupdescription("uptime"));

}

31 }

重點是28、29行,有了這兩句,listbox就能準確得分組顯示了,其他itemscontrol的分組類同。

WPF裡ItemsControl的分組實現

原文 wpf裡itemscontrol的分組實現 我們在用到itemscontrol時,有時會用到分組,如listbox,listview,datagrid。wpf的itemscontrol可以實現分組,是依託於groupstyle,以listbox為例,他的分組效果圖為 以下為前台 1 lbmai...

WPF裡實現imageButton的步驟

1 建立乙個新的類庫並寫乙個類 imagebutton,如下 using system using system.collections.generic using system.linq using system.text using system.windows using system.win...

WPF學習筆記 在XAML裡繫結

binding除了預設建構函式外,還有乙個可以傳入path的建構函式,下面兩種方式實現的功能是一樣的。text backgroud aliceblue fontsize 16 text backgroud aliceblue fontsize 16 這裡使用elementname來設定源物件,而沒有...