日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
WPF數(shù)據(jù)綁定在目錄樹(shù)構(gòu)造中作用體現(xiàn)

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

創(chuàng)新互聯(lián)專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、網(wǎng)站建設(shè)、夾江網(wǎng)絡(luò)推廣、成都小程序開(kāi)發(fā)、夾江網(wǎng)絡(luò)營(yíng)銷(xiāo)、夾江企業(yè)策劃、夾江品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪(fǎng)、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供夾江建站搭建服務(wù),24小時(shí)服務(wù)熱線(xiàn):028-86922220,官方網(wǎng)址:www.cdcxhl.com

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

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

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

  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. //計(jì)算出目錄名稱(chēng)(不包含路徑)
  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. }
  51. }

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

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

我們可以在Window的代碼里設(shè)置界面的DataContext,ItemsSource等屬性來(lái)讓界面顯示指定的數(shù)據(jù),也可以構(gòu)造一個(gè)專(zhuān)門(mén)提供數(shù)據(jù)的類(lèi),完全在界面(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. }
  15. }

這個(gè)類(lèi)就更簡(jiǎn)單了,僅僅是在創(chuàng)建的時(shí)候加載所有的磁盤(pán)的根目錄.

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

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

 
 
 
  1. < !--xml:sample這一行用來(lái)引入
    我們自己代碼的命名空間-->
  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ù)提供對(duì)象-->
  9. < ObjectDataProvider x:Key="drives" 
    ObjectType="{x:Type sample:
    BindDirectoryList}" />
  10. < !--設(shè)置如何顯示數(shù)據(jù),以及如何獲
    取下一級(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>

這里我們?cè)赬AML里定義了一個(gè)drives對(duì)象,它的類(lèi)型為BindDirectoryList,創(chuàng)建時(shí)會(huì)自動(dòng)加載磁盤(pán)的根目錄;

我們?cè)赪PF數(shù)據(jù)綁定中還定義了一個(gè)針對(duì)BindDirectory類(lèi)型的層次型數(shù)據(jù)模板itemsTemplate,指定了要獲取此類(lèi)型的數(shù)據(jù)的子數(shù)據(jù)需要通過(guò)Directories屬性,并且告訴WPF用一個(gè)TextBlock來(lái)顯示它的名稱(chēng).

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


文章標(biāo)題:WPF數(shù)據(jù)綁定在目錄樹(shù)構(gòu)造中作用體現(xiàn)
本文地址:http://m.5511xx.com/article/ccocphe.html