Skip to content

Commit 18bbaec

Browse files
authored
Merge pull request #12 from olangness/ModelToViewModelBinding
Getting the link view to work
2 parents e8c1984 + 8441f7f commit 18bbaec

File tree

7 files changed

+62
-62
lines changed

7 files changed

+62
-62
lines changed

WebCrawler/Model/Crawler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Crawler
2525
private static List<string> _exceptions = new List<string>();
2626
private bool isCurrentPage = true;
2727
private static List<string> urlsWithTopics = new List<string>();
28-
private static List<Link> links = new List<Link>();
28+
private static ObservableCollection<Link> links = new ObservableCollection<Link>();
2929
private static ObservableCollection<Log> log = new ObservableCollection<Log>();
3030

3131
private static Crawler instance = null;
@@ -56,7 +56,7 @@ public ObservableCollection<Log> Log
5656
set { log = value; }
5757
}
5858

59-
public List<Link> Links
59+
public ObservableCollection<Link> Links
6060
{
6161
get { return links; }
6262
set { links = value; }
@@ -111,7 +111,7 @@ private void CrawlPage(string url)
111111
page.Url = url;
112112

113113
_pages.Add(page);
114-
//links.Add(url);
114+
links.Add(new Link("Page Title", url));
115115
log.Add(new Log($"New Entry: {url}", DateTime.Now));
116116

117117
linkParser.ParseLinks(page, url);

WebCrawler/Model/Data Storage/Data.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,8 @@ public Topic GetSpecificTopic(string name, string[] urls) // get a specific topi
117117

118118
public void AddLink(string name, string url, int linkID, int parentID) // add a link to the list
119119
{
120-
myLink.Name = name;
121-
myLink.URL = url;
122-
myLink.LinkID = linkID;
123-
myLink.ParentID = parentID;
120+
myLink.LinkName = name;
121+
myLink.LinkURL = url;
124122

125123
for (int i = 0; i < links.Length; i++)
126124
{
@@ -152,10 +150,10 @@ public Link[] GetLinks() // get the list of links
152150

153151
public Link GetSpecificLink(string name, string url, int linkID, int parentID)
154152
{
155-
Link temp = new Link("no Link found", "", 0, 0);
153+
Link temp = new Link("no Link found", "");
156154
for (int i = 0; i < links.Length; i++)
157155
{
158-
if (links[i].Name.Equals(name) && links[i].URL.Equals(url) && links[i].LinkID == linkID && links[i].ParentID == parentID)
156+
if (links[i].LinkName.Equals(name) && links[i].LinkURL.Equals(url))
159157
{
160158
temp = links[i];
161159
}

WebCrawler/Model/Data Storage/Link.cs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,55 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
67

78
namespace WebCrawler.Model
89
{
9-
public class Link
10+
public class Link : INotifyPropertyChanged
1011
{
11-
string name;
12-
string url;
13-
int linkID;
14-
int parentID;
1512

1613
public Link()
1714
{
1815

1916
}
20-
public Link(string name, string url, int linkID, int parentID)
17+
public Link(string name, string url)
2118
{
22-
Name = name;
23-
URL = url;
24-
LinkID = linkID;
25-
ParentID = parentID; //if parentID == 0, then this is root URL
19+
LinkName = name;
20+
LinkURL = url;
2621
}
2722

28-
public string Name
23+
private string _linkName;
24+
public string LinkName
2925
{
30-
get { return name; }
31-
32-
set { name = value; }
26+
get { return _linkName; }
27+
set
28+
{
29+
_linkName = value;
30+
NotifyPropertyChanged("LinkName");
31+
}
3332
}
3433

35-
public string URL
34+
private string _linkUrl;
35+
public string LinkURL
3636
{
37-
get { return url; }
38-
39-
set { url = value; }
37+
get { return _linkUrl; }
38+
set
39+
{
40+
_linkUrl = value;
41+
NotifyPropertyChanged("LinkURL");
42+
}
4043
}
4144

42-
public int LinkID
43-
{
44-
get { return linkID; }
45-
set { linkID = value; }
46-
}
45+
public event PropertyChangedEventHandler PropertyChanged;
4746

48-
public int ParentID
47+
private void NotifyPropertyChanged(String info)
4948
{
50-
get { return parentID; }
51-
set { parentID = value; }
49+
if (PropertyChanged != null)
50+
{
51+
PropertyChanged(this, new PropertyChangedEventArgs(info));
52+
}
5253
}
53-
54-
55-
56-
5754
}
5855
}

WebCrawler/Model/Data Storage/Log.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace WebCrawler.Model
1010
{
11-
public class Log : ILog
11+
public class Log : INotifyPropertyChanged
1212
{
1313
string messageText;
1414
DateTime messageTime;

WebCrawler/View/WebsiteTree/WebsiteTreeTabView.xaml

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,13 @@
66
xmlns:local="clr-namespace:WebCrawler.View.WebsiteTree"
77
mc:Ignorable="d"
88
d:DesignHeight="450" d:DesignWidth="800">
9-
<Grid>
10-
11-
<Grid.RowDefinitions>
12-
<RowDefinition Height="2*"></RowDefinition>
13-
<RowDefinition Height="23*"></RowDefinition>
14-
</Grid.RowDefinitions>
15-
<Button Width="60" Grid.Row="0" HorizontalAlignment="Left" Command="{Binding Path=Refresh, Mode=OneWay}">Refresh</Button>
16-
<TreeView Name="trvMenu" Grid.Row="1">
17-
<TreeView.ItemTemplate>
18-
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
19-
<TextBlock Text="{Binding Title}" />
20-
</HierarchicalDataTemplate>
21-
</TreeView.ItemTemplate>
22-
</TreeView>
23-
</Grid>
9+
<ListView Grid.Column="1" Name="ListViewProjects" Grid.RowSpan="3" SelectionChanged="ListViewLinkSelectionChanged" ItemsSource="{Binding Links}" IsSynchronizedWithCurrentItem="True" MinWidth="100">
10+
<ListView.ItemTemplate>
11+
<DataTemplate>
12+
<WrapPanel>
13+
<TextBlock Text="{Binding Path=LinkURL}"/>
14+
</WrapPanel>
15+
</DataTemplate>
16+
</ListView.ItemTemplate>
17+
</ListView>
2418
</UserControl>

WebCrawler/View/WebsiteTree/WebsiteTreeTabView.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public WebsiteTreeTabView()
3030
// The DataContext serves as the starting point of Binding Paths
3131
DataContext = _viewModel;
3232
}
33+
34+
public void ListViewLinkSelectionChanged(object sender, SelectionChangedEventArgs e)
35+
{
36+
}
3337
}
3438
}
3539

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
using GalaSoft.MvvmLight.Command;
22
using System;
33
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
using System.ComponentModel;
46
using System.Linq;
57
using System.Text;
68
using System.Threading.Tasks;
79
using System.Windows.Input;
10+
using WebCrawler.Model;
811
using WebCrawler.View.WebsiteTree;
912
using static System.Windows.Forms.LinkLabel;
1013

1114
namespace WebCrawler.ViewModel.WebsiteTree
1215
{
13-
class WebsiteTreeTabViewModel
16+
class WebsiteTreeTabViewModel : INotifyPropertyChanged
1417
{
15-
public string WebsiteURL { get; set; }
1618

17-
18-
public ICommand Refresh { get; private set; }
19+
public ObservableCollection<Model.Link> Links { get; set; } = new ObservableCollection<Model.Link>();
1920

2021
public WebsiteTreeTabViewModel()
2122
{
22-
Refresh = new RelayCommand<object>(_ => RefreshTree());
23+
Links = Crawler.Instance.Links;
2324
}
2425

25-
public void RefreshTree()
26+
public event PropertyChangedEventHandler PropertyChanged;
27+
28+
protected void OnPropertyChange(string propertyName)
2629
{
27-
Link parent = new Link();
30+
if (PropertyChanged != null)
31+
{
32+
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
33+
}
2834
}
35+
2936
}
3037
}

0 commit comments

Comments
 (0)