<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:WpfApplication3"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<HierarchicalDataTemplate DataType="{x:Type data:Root}" ItemsSource="{Binding Path=WebPages}">
<StackPanel>
<TextBlock Text="{Binding Path=Title}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type data:WebPage}" ItemsSource="{Binding Path=LinksInPage}" >
<TextBlock Text="{Binding Path=PageTitle}"></TextBlock>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView Margin="0,0,15,0" Name="treeView1" Background="LightSkyBlue">
<TreeViewItem ItemsSource="{Binding Path=WebPages}"
Header="{Binding Title}"></TreeViewItem>
</TreeView>
</Grid>
</Window>
Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Root r = new Root();
r.WebPages = new List<WebPage>();
r.Title = "HomePage";
r.Url = @"http://www.HomePage.com";
WebPage link1 = new WebPage();
link1.Href = @"http://www.HomePage.com/link1";
link1.PageTitle = "link1";
r.WebPages.Add(link1);
WebPage Link2 = new WebPage();
Link2.Href = @"http://www.HomePage.com/Link2";
Link2.PageTitle = "Link2";
r.WebPages.Add(Link2);
WebPage Link3 = new WebPage();
Link3.Href = @"http://www.HomePage.com/Link2/Link3";
Link3.PageTitle = "Link3";
Link2.LinksInPage = new List<WebPage>();
Link2.LinksInPage.Add(Link3);
WebPage Link4 = new WebPage();
Link4.Href = @"http://www.HomePage.com/Link2/Link4";
Link4.PageTitle = "Link4";
Link2.LinksInPage.Add(Link4);
WebPage Link5 = new WebPage();
Link5.Href = @"http://www.HomePage.com/link1/Link5";
Link5.PageTitle = "Link5";
link1.LinksInPage = new List<WebPage>();
link1.LinksInPage.Add(Link5);
WebPage Link6 = new WebPage();
Link6.Href = @"http://www.HomePage.com/link2/Link3/Link6";
Link6.PageTitle = "Link6";
Link3.LinksInPage = new List<WebPage>();
Link3.LinksInPage.Add(Link6);
treeView1.DataContext = r;
}
}
public class WebPage
{
public string Href { get; set; }
public string PageTitle { get; set; }
public List<WebPage> LinksInPage { get; set; }
}
public class Root
{
public string Title { get; set; }
public string Url { get; set; }
public List<WebPage> WebPages { get; set; }
}
}
No comments:
Post a Comment