silverlight动态加载treeview

晚上练习下SL3.0,自己写了用XML动态加载TREEVIEW;希望能对大家有所帮助.

 

private void InitData()
        {
            XElement root = XElement.Load("SiteMap.xml");

            var result = LoadData(root);

            treeView.DataContext = result;
        }

        private List<TreeViewModel> LoadData(XElement root)
        {
            if (root == null)
                return null;

            var items = from n in root.Elements("node")
                        select new TreeViewModel
                        {
                            Title = (string)n.Attribute("name"),
                            Address = new Uri((string)n.Attribute("address"), UriKind.Relative),
                            Children = LoadData(n)
                        };

            return items.ToList();
        }

        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (((TreeViewModel)e.NewValue).Address.ToString() != "")
                frame.Navigate(((TreeViewModel)e.NewValue).Address);
        }

xml文件还是给点嘛  呵呵;

<?xml version="1.0" encoding="utf-8" ?>
<root>
    <node name="控件(Control)" address="">
        <node name="AutoCompleteBox" address="/Control/AutoCompleteBox.xaml" />
        <node name="DataPager" address="/Control/DataPager.xaml" />
        <node name="Frame" address="/Control/Frame.xaml" />
        <node name="Page" address="/Control/PageDemo.xaml" />
        <node name="Label" address="/Control/Label.xaml" />
        <node name="DescriptionViewer" address="/Control/DescriptionViewer.xaml" />
        <node name="ValidationSummary" address="/Control/ValidationSummary.xaml" />
        <node name="ChildWindow" address="/Control/ChildWindowDemo.xaml" />
        <node name="SaveFileDialog" address="/Control/SaveFileDialogDemo.xaml" />
        <node name="HeaderedItemsControl" address="/Control/HeaderedItemsControl.xaml" />
        <node name="VirtualizingStackPanel" address="/Control/VirtualizingStackPanel.xaml" />
        <node name="TreeView" address="/Control/TreeView.xaml" />
        <node name="ListBox" address="/Control/ListBox.xaml" />
        <node name="DataGrid" address="/Control/DataGrid.xaml" />
        <node name="MediaElement" address="/Control/MediaElement.xaml" />
    </node>.......

posted @ 2009-11-05 21:02  TMAC  阅读(1201)  评论(2编辑  收藏  举报