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會提示找不到;

直到這裏,我們的工作基本上算是完成了,效果如下:

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章