I'm building WPF application which downloads links to articles from website and displays them in a grid.
Summary of the problem:
The problem I'm hitting is that I have downloading logic in the model which does not have access to the ObservableCollection Articles (which is defined in my ViewModel and bound to DataGrid).
How should I access this collection to update it as I'm downloading new articles (so that data grid will keep adding new ones one by one)?
Details:
Here's overview of my app which implements MVVM pattern (I cut some of the not important parts to keep it easy to read):
DisplayWindow.xaml Is binding source to PresenterViewModel.cs and ItemSource is set to Articles which is ObservableCollection (see below)
<Grid DataContext="{Binding Source={StaticResource presenterViewModel}}">
<DataGrid ItemsSource="{Binding Articles}">
<DataGrid.Columns>
<DataGridTextColumn Header="Url" Binding="{Binding Url}"/>
...
</DataGrid.Columns>
</DataGrid>
</Grid>
it also has button which triggers downloading of the article links via
<Button Content="Download" Command="{Binding DownloadArticles, Mode=OneWay}"/>
DownloadArticles method is part of PresenterViewModel and returns instance of DownloadCommand which implements interface ICommand.
PresenterViewModel.cs
contains ObservableCollection<Article> Articles
private ObservableCollection<Article> articles;
public ObservableCollection<Article> Articles
{
get { return articles; }
set
{
articles = value;
RaisePropertyChangedEvent("Articles");
}
}
public ICommand DownloadArticles
{
get
{
return downloadCommand;
}
}
DownloadCommand contains reference to PresenterViewModel and in Execute method calls it's method DownloadArticles which calls DownloadArticles in the Article model itself.
in DownloadCommand.cs:
public void Execute(object parameter)
{
presenterViewModel.DownloadArticles();
}
Solutions I was considering:
Now, the problem is I'm in DownloadArticles method in Article.cs model and need to update Articles ObservableCollection which is part of PresenterViewModel... how do I do that? The DownloadArticles method runs downloading logic in a separate thread.
Should I pass the reference to PresenterViewModel to the DownloadArticles method of the model?
This seems like an easy way to do it, however it just doesn't feel right - I don't think model should be coupled to the ViewModel and in this case one of the methods would accept PresenterViewModel object.
Another option would be having downloading logic directly in PresenterViewModel, but that doesn't feels right either as I'd prefer my ViewModel to be lightweight and not containing any logic.
What would be the best solution in this case? If you think my architecture is fundamentally wrong, please let me know what would be the best way of structuring it in this case.
I really appreciate your advices on this!
Commandshould be in yourVMand your downloader should be in yourModel. YourVMshould have a reference to your Model. This way there is no problem in communication between Model and ViewModel and also this is how MvvM works.