偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

WPF數(shù)據(jù)綁定在目錄樹構(gòu)造中作用體現(xiàn)

開發(fā) 開發(fā)工具
當(dāng)我們使用WPF數(shù)據(jù)綁定創(chuàng)建一個目錄樹的時候,可以分為三步實現(xiàn),分別為:定義要綁定的數(shù)據(jù)類型;創(chuàng)建一個數(shù)據(jù)提供類;設(shè)計用戶界面。

WPF開發(fā)工具的使用,為開發(fā)人員帶來了非常大的作用。開發(fā)人員在實際開發(fā)編程中,可以輕松靈活的實現(xiàn)與MAC相媲美的圖形界面。#t#

如果使用了WPF而不使用數(shù)據(jù)綁定(手工在界面和數(shù)據(jù)間進(jìn)行同步),總會感覺不值.但是大部分討論WPF數(shù)據(jù)綁定的文章,主題大多集中在ListBox這樣平坦的數(shù)據(jù)集合上,講如何綁定層次結(jié)構(gòu)數(shù)據(jù)的比較少,這里我就通過一個簡單的顯示磁盤目錄樹的例子來展示如何完成這樣的任務(wù).

WPF數(shù)據(jù)綁定第一步,當(dāng)然是定義要綁定的數(shù)據(jù)類型了.

在目錄樹這個例子中,每個TreeViewItem要顯示的數(shù)據(jù)可以用System.IO.DirectoryInfo來表示,但是這樣做有一個麻煩:DirectoryInfo只能通過GetDirectories()方法來獲取子目錄,但是WPF里的數(shù)據(jù)綁定則更傾向于使用屬性在數(shù)據(jù)間導(dǎo)航,所以為了更方便地使用WPF數(shù)據(jù)綁定,我們最好還是自定義一個類來完成這樣的工作:

  1. using System.Collections.Generic;  
  2. using System.IO;  
  3. namespace WpfApplication1  
  4. {  
  5. class BindDirectory  
  6. {  
  7. public BindDirectory(string 
    directoryPath)  
  8. {  
  9. //正規(guī)化目錄路徑,確保Path以'\\'結(jié)尾  
  10. directoryPathdirectoryPath = 
    directoryPath.TrimEnd('\\');  
  11. Path = directoryPath + '\\';  
  12. //計算出目錄名稱(不包含路徑)  
  13. int indexLastSlash = directoryPath.
    LastIndexOf('\\');  
  14. if (indexLastSlash >= 0)  
  15. {  
  16. Name = directoryPath.Substring
    (indexLastSlash + 1);  
  17. }  
  18. else  
  19. {  
  20. Name = directoryPath;  
  21. }  
  22. }  
  23. public string Name  
  24. {  
  25. get;  
  26. private set;  
  27. }  
  28. public string Path  
  29. {  
  30. get;  
  31. private set;  
  32. }  
  33. public IEnumerable< BindDirectory> 
    Directories  
  34. {  
  35. get  
  36. {  
  37. //延遲加載  
  38. if (directories == null)  
  39. {  
  40. directories = new List
    < BindDirectory>();  
  41. foreach (string d in Directory.
    GetDirectories(Path))  
  42. {  
  43. directories.Add(new 
    BindDirectory(d));  
  44. }  
  45. }  
  46. return directories;  
  47. }  
  48. }  
  49. List< BindDirectory> directories;  
  50. }  

 

這個類所作的工作很簡單,就是正規(guī)化目錄路徑,獲取目錄名稱,以及延遲加載子目錄(以提升性能)的列表,我們的界面也只要求它具有這些功能就行了.

WPF數(shù)據(jù)綁定第二步,創(chuàng)建一個數(shù)據(jù)提供類(DataProvider)

我們可以在Window的代碼里設(shè)置界面的DataContext,ItemsSource等屬性來讓界面顯示指定的數(shù)據(jù),也可以構(gòu)造一個專門提供數(shù)據(jù)的類,完全在界面(XAML)里指定,這里使用的是第二種方法:

  1. using System.Collections.Generic;  
  2. using System.IO;  
  3. namespace WpfApplication1  
  4. {  
  5. class BindDirectoryList : 
    List
    < BindDirectory> 
  6. {  
  7. public BindDirectoryList()  
  8. {  
  9. foreach (var drive in 
    DriveInfo.GetDrives())  
  10. {  
  11. Add(new BindDirectory(drive.
    RootDirectory.FullName));  
  12. }  
  13. }  
  14. }  

 

這個類就更簡單了,僅僅是在創(chuàng)建的時候加載所有的磁盤的根目錄.

WPF數(shù)據(jù)綁定第三步,設(shè)計用戶界面

只需要在Window中添加一個TreeView,然后修改幾行代碼,就能輕松地顯示我們的數(shù)據(jù)了:

  1. < !--xml:sample這一行用來引入
    我們自己代碼的命名空間--
    > 
  2. < Window x:Class="WpfApp
    lication1.Window1"
     
  3. xmlns="http://schemas.
    microsoft.com/winfx/2006/
    xaml/presentation"
     
  4. xmlns:x="http://schemas.
    microsoft.com/winfx/2006/xaml"
     
  5. xmlns:sample="clr-namespace:
    WpfApplication1"
     
  6. Title="Window1" Height="300" 
    Width="300"> 
  7. < Window.Resources> 
  8. < !--引入我們自己的數(shù)據(jù)提供對象--> 
  9. < ObjectDataProvider x:Key="drives" 
    ObjectType="{x:Type sample:
    BindDirectoryList}"
     /> 
  10. < !--設(shè)置如何顯示數(shù)據(jù),以及如何獲
    取下一級數(shù)據(jù)的列表--
    > 
  11. < HierarchicalDataTemplate x:Key=
    "itemTemplate" DataType="{x:Type 
    sample:BindDirectory}"
     ItemsSource=
    "{Binding Directories}"> 
  12. < TextBlock Text="{Binding Name}" /> 
  13. < /HierarchicalDataTemplate> 
  14. < /Window.Resources> 
  15. < TreeView ItemsSource="{Binding 
    Source={StaticResource drives}}"
     
  16. ItemTemplate="{StaticResource 
    itemTemplate}"
     > 
  17. < /TreeView> 
  18. < /Window> 

這里我們在XAML里定義了一個drives對象,它的類型為BindDirectoryList,創(chuàng)建時會自動加載磁盤的根目錄;

我們在WPF數(shù)據(jù)綁定中還定義了一個針對BindDirectory類型的層次型數(shù)據(jù)模板itemsTemplate,指定了要獲取此類型的數(shù)據(jù)的子數(shù)據(jù)需要通過Directories屬性,并且告訴WPF用一個TextBlock來顯示它的名稱.

最后,我們設(shè)置一下TreeView的ItemsSource和ItemTemplate就完成工作了.

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2009-12-24 16:57:53

WPF密碼

2009-12-29 14:00:02

WPF Dispatc

2009-12-24 17:52:05

WPF觸發(fā)器

2023-10-07 11:04:58

WPF數(shù)據(jù)UI

2009-12-25 16:40:49

WPF優(yōu)勢

2021-02-11 08:27:28

數(shù)據(jù)

2009-12-24 11:15:59

WPF數(shù)據(jù)綁定

2009-12-29 14:58:31

WPF優(yōu)點

2009-12-23 15:16:52

WPF數(shù)據(jù)綁定

2010-02-23 16:15:24

WCF Endpoin

2009-12-28 16:45:31

WPF窗體

2009-12-23 15:57:40

WPF傳遞事件

2009-12-25 15:29:12

WPF缺陷

2011-03-30 09:13:13

靜態(tài)類Windows Pho

2009-11-25 17:54:47

PHP數(shù)組函數(shù)

2010-02-02 13:15:00

C++ lambda函

2010-02-25 17:22:39

WCF服務(wù)行為

2010-01-14 10:35:34

VB.NET指針

2009-12-04 17:31:32

PHP編碼轉(zhuǎn)換

2010-03-01 17:52:03

WCF選擇綁定
點贊
收藏

51CTO技術(shù)棧公眾號