Silverlight TreeView 动态绑定Xml 文件

随着应用程序的不断升级,客户的需求不断增多,程序员不得不对自己的应用程序做出相应的修改,如果修改的内容较多,那么就必须找出一种简便方法,下面就为大家介绍一下在SilverLight 中左边导航栏TreeView 如何动态绑定 Xml 文件中的数据

1、准备工作,首先建立一个TreeViewData.xml文件,代码如下:

 

 
 
  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. <root> 
  3.   <node name="系统管理"> 
  4.     <node name="添加用户"/> 
  5.     <node name="用户管理"/> 
  6.     <node name="修改密码"/> 
  7.     <node name="系统参数"/> 
  8.   </node> 
  9.   <node name="操作管理"> 
  10.     <node name="违法数据录入"/> 
  11.     <node name="违法信息套打" /> 
  12.     <node name="业务办理" /> 
  13.   </node> 
  14. </root> 

2、建立一个TreeViewLoadXmlTest.xaml文件并在其中添加如下代码:

 

 
 
  1. <navigation:Page x:Class="MySilverLight.TreeViewLoadXmlTest"   
  2.            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3.            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  4.            xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5.            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6.                  xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls" 
  7.                  xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"    
  8.            mc:Ignorable="d" 
  9.            xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
  10.            d:DesignWidth="640" d:DesignHeight="480" 
  11.            Title="TreeViewLoadXmlTest Page"> 
  12.     <Grid x:Name="LayoutRoot"> 
  13.         <StackPanel Background="#ffc"> 
  14.             <StackPanel.Resources> 
  15.                 <common:HierarchicalDataTemplate 
  16.                     x:Key="childTemplate" 
  17.                     ItemsSource="{Binding Path=Children}"> 
  18.                     <StackPanel> 
  19.                         <TextBlock Text="{Binding Path=Title}" 
  20.                                  FontStyle="Italic"/> 
  21.                     </StackPanel> 
  22.                 </common:HierarchicalDataTemplate> 
  23.                 <common:HierarchicalDataTemplate 
  24.                     x:Key="treeTemplate" 
  25.                     ItemsSource="{Binding Path=Children}" 
  26.                     ItemTemplate="{StaticResource childTemplate}"> 
  27.                     <TextBlock Text="{Binding Path=Title}" 
  28.                                FontWeight="Bold"/> 
  29.                 </common:HierarchicalDataTemplate> 
  30.             </StackPanel.Resources> 
  31.             <!--  
  32.                 ItemsSource - 数据源  
  33.                 ItemTemplate - 指定层级显示数据的模板  
  34.             --> 
  35.             <controls:TreeView x:Name="treeView" Margin="5" 
  36.                 ItemsSource="{Binding}"   
  37.                 ItemTemplate="{StaticResource treeTemplate}" 
  38.                 SelectedItemChanged="treeView_SelectedItemChanged"> 
  39.             </controls:TreeView> 
  40.         </StackPanel> 
  41.     </Grid> 
  42. </navigation:Page> 

值得注意的是,在写代码之前,需要在头部加上这样两句话:

xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
                 xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"

否则,在后面会提示common 和controls出错

 

3、接下来需要准备一个TreeViewModel.cs类,代码如下:

 

 
 
  1. namespace MySilverLight  
  2. {  
  3.     public class TreeViewModel  
  4.     {  
  5.         public string Title { get; set; }  
  6.         public Uri Address { get; set; }  
  7.         public List<TreeViewModel> Children { get; set; }  
  8.     }  

在此需要引入using System.Collections.Generic;

 

4、打开后台代码文件TreeViewLoadXmlTest.xaml.cs ,代码如下:

 

 
 
  1. namespace MySilverLight  
  2. {  
  3.     public partial class TreeViewLoadXmlTest : Page  
  4.     {  
  5.         public TreeViewLoadXmlTest()  
  6.         {  
  7.             InitializeComponent();  
  8.             this.Loaded += new RoutedEventHandler(TreeView_Loaded);  
  9.         }  
  10.  
  11.         // 当用户导航到此页面时执行。  
  12.         protected override void OnNavigatedTo(NavigationEventArgs e)  
  13.         {  
  14.         }  
  15.         void TreeView_Loaded(object sender, RoutedEventArgs e)  
  16.         {  
  17.             XElement root = XElement.Load("TreeViewData.xml");  
  18.  
  19.             // 构造带层级关系的数据源(递归方式)  
  20.             var result = LoadData(root);  
  21.  
  22.             treeView.DataContext = result;  
  23.         }  
  24.         private List<TreeViewModel> LoadData(XElement root)  
  25.         {  
  26.             if (root == null)  
  27.                 return null;  
  28.  
  29.             var items = from n in root.Elements("node")  
  30.                         select new TreeViewModel  
  31.                         {  
  32.                             Title = (string)n.Attribute("name"),  
  33.                             Children = LoadData(n)  
  34.                         };  
  35.  
  36.             return items.ToList();  
  37.         }  
  38.         private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)  
  39.         {  
  40.             MessageBox.Show(((TreeViewModel)e.NewValue).Title);  
  41.         }  
  42.     }  

这里同样值得注意的是,需要引用命名空间,System.Xml.Linq;否则XElement会提示找不到;

直到这里,我们的工作基本上算是完成了,效果如下:

 

 

 

本文出自 “程序人生_意念” 博客,请务必保留此出处http://07180402.blog.51cto.com/3679519/987599

猜你喜欢

转载自blog.csdn.net/cs9dn003/article/details/53114237