成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

WPF開發(fā)工具的使用,為開發(fā)人員帶來了非常大的作用。開發(fā)人員在實(shí)際開發(fā)編程中,可以輕松靈活的實(shí)現(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

2023-10-07 11:04:58

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

2009-12-24 17:52:05

WPF觸發(fā)器

2009-12-25 16:40:49

WPF優(yōu)勢

2009-12-29 14:58:31

WPF優(yōu)點(diǎn)

2009-12-24 11:15:59

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

2021-02-11 08:27:28

數(shù)據(jù)

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-25 15:29:12

WPF缺陷

2009-12-23 15:57:40

WPF傳遞事件

2011-03-30 09:13:13

靜態(tài)類Windows Pho

2010-01-14 10:35:34

VB.NET指針

2009-12-04 17:31:32

PHP編碼轉(zhuǎn)換

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-03-01 17:52:03

WCF選擇綁定
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 国产成人久久精品一区二区三区 | h视频在线播放 | 国产一级片久久久 | 亚洲精品视频播放 | 午夜小视频免费观看 | 色视频在线播放 | 毛片黄片免费看 | 欧美精品a∨在线观看不卡 欧美日韩中文字幕在线播放 | 日韩网站免费观看 | 久久久久国产精品 | 99精品一区二区三区 | 欧美精品久久 | 影音先锋欧美资源 | 国产成人免费一区二区60岁 | 性福视频在线观看 | 日韩在线播放第一页 | 亚洲一区精品在线 | 日韩在线精品视频 | 51ⅴ精品国产91久久久久久 | 午夜免费精品视频 | 国产一区二区三区四区 | av福利网站 | av电影一区二区 | 国产女人与拘做受免费视频 | 三级国产三级在线 | 欧美一区二区在线观看 | 精品在线| 日本一区二区影视 | 一区欧美 | 亚洲一区自拍 | 国产一区二区在线播放 | 久久久久网站 | 国产精品久久久久久久久久久久冷 | 日韩中文字幕在线视频 | 免费黄色录像视频 | aa级毛片毛片免费观看久 | 亚洲国产精品久久久久久 | 日韩中文字幕在线视频 | 精品久久久久久亚洲综合网 | 色欧美片视频在线观看 | 一区二区三区日 |