WPF数据绑定在目录树构造中作用体现

开发 开发工具
当我们使用WPF数据绑定创建一个目录树的时候,可以分为三步实现,分别为:定义要绑定的数据类型;创建一个数据提供类;设计用户界面。

WPF开发工具的使用,为开发人员带来了非常大的作用。开发人员在实际开发编程中,可以轻松灵活的实现与MAC相媲美的图形界面。#t#

如果使用了WPF而不使用数据绑定(手工在界面和数据间进行同步),总会感觉不值.但是大部分讨论WPF数据绑定的文章,主题大多集中在ListBox这样平坦的数据集合上,讲如何绑定层次结构数据的比较少,这里我就通过一个简单的显示磁盘目录树的例子来展示如何完成这样的任务.

WPF数据绑定第一步,当然是定义要绑定的数据类型了.

在目录树这个例子中,每个TreeViewItem要显示的数据可以用System.IO.DirectoryInfo来表示,但是这样做有一个麻烦:DirectoryInfo只能通过GetDirectories()方法来获取子目录,但是WPF里的数据绑定则更倾向于使用属性在数据间导航,所以为了更方便地使用WPF数据绑定,我们最好还是自定义一个类来完成这样的工作:

  1. using System.Collections.Generic;  
  2. using System.IO;  
  3. namespace WpfApplication1  
  4. {  
  5. class BindDirectory  
  6. {  
  7. public BindDirectory(string 
    directoryPath)  
  8. {  
  9. //正规化目录路径,确保Path以'\\'结尾  
  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. }  

 

这个类所作的工作很简单,就是正规化目录路径,获取目录名称,以及延迟加载子目录(以提升性能)的列表,我们的界面也只要求它具有这些功能就行了.

WPF数据绑定第二步,创建一个数据提供类(DataProvider)

我们可以在Window的代码里设置界面的DataContext,ItemsSource等属性来让界面显示指定的数据,也可以构造一个专门提供数据的类,完全在界面(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. }  

 

这个类就更简单了,仅仅是在创建的时候加载所有的磁盘的根目录.

WPF数据绑定第三步,设计用户界面

只需要在Window中添加一个TreeView,然后修改几行代码,就能轻松地显示我们的数据了:

  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. < !--引入我们自己的数据提供对象--> 
  9. < ObjectDataProvider x:Key="drives" 
    ObjectType="{x:Type sample:
    BindDirectoryList}"
     /> 
  10. < !--设置如何显示数据,以及如何获
    取下一级数据的列表--
    > 
  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,创建时会自动加载磁盘的根目录;

我们在WPF数据绑定中还定义了一个针对BindDirectory类型的层次型数据模板itemsTemplate,指定了要获取此类型的数据的子数据需要通过Directories属性,并且告诉WPF用一个TextBlock来显示它的名称.

最后,我们设置一下TreeView的ItemsSource和ItemTemplate就完成工作了.

责任编辑:曹凯 来源: 博客园
相关推荐

2009-12-24 16:57:53

WPF密码

2009-12-29 14:00:02

WPF Dispatc

2009-12-24 17:52:05

WPF触发器

2023-10-07 11:04:58

WPF数据UI

2009-12-25 16:40:49

WPF优势

2009-12-29 14:58:31

WPF优点

2021-02-11 08:27:28

数据

2009-12-24 11:15:59

WPF数据绑定

2009-12-23 15:16:52

WPF数据绑定

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

静态类Windows Pho

2009-11-25 17:54:47

PHP数组函数

2010-02-02 13:15:00

C++ lambda函

2010-02-25 17:22:39

WCF服务行为

2009-12-04 17:31:32

PHP编码转换

2010-01-14 10:35:34

VB.NET指针

2010-03-01 17:52:03

WCF选择绑定
点赞
收藏

51CTO技术栈公众号