Reputation: 3
In the application I'm building. I seem to be having the problem that my previous inserted ObservableCollection for Categories, doesn't seem to be transferred from ViewModel to Viewmodel.
I've tried putting a public Category CategoryToEdit { get; set; } reference to bind it towards my type as CategoryToEdit.Type Previously this worked for me to connect the selected data from another viewmodel. But this happened on a template without mvvm, and I can't seem to find the working solution to on it.
This is my MainWindowViewModel:
namespace Webadmin.ViewModels {
public class MainWindowViewModel
{
public ObservableCollection<Category> Categories { get; set; }
public RelayCommand ChangeCategoryClick { get; set; }
public MainWindowViewModel()
{
Categories = new ObservableCollection<Category>();
ChangeCategoryClick = new RelayCommand(f => ChangeCategory(), null);
Categories.Add(new Category() { Type = "Electronics" });
Categories.Add(new Category() { Type = "Clothes" });
Categories.Add(new Category() { Type = "Books" });
}
private void ChangeCategory()
{
FilterWindow changeWindow = new FilterWindow
{
};
changeWindow.Show();
}
}
}
This ChangeCategory is simply bound to a command on a button so it can change window. Button Content="Change" Command="{Binding ChangeCategoryClick}"
This is my FilterWindowViewModel (where the previous filled in types should be displayed):
namespace Webadmin.ViewModels
{
class FilterWindowViewModel
{
public ObservableCollection<Category> Categories { get; set; }
public ICollectionView CategoryView { get; set; }
public FilterWindowViewModel()
{
Categories = new ObservableCollection<Category>();
CategoryView = CollectionViewSource.GetDefaultView(Categories);
CategoryView.SortDescriptions.Add(new SortDescription("Type", ListSortDirection.Ascending));
}
}
}
I try to display my categories in FilterWindow Datagrid by using
DataGrid ItemsSource="{Binding Categories}"
But all I end up with is an empty table
The empty table should of course be created due to it being newly recreated in the FilterWindowViewModel. But what I want to have is let it be connected to main filled in categories. With that connection, I can add, delete, edit those categories so that my products bound those current categories could have the possibilty to have more/less/different to choose from.
Upvotes: 0
Views: 62
Reputation: 1322
This is where an MVVM framework can assist. For example, in MVVM Light, there is the concept of the Messenger that facilitates the communication between View Models for this exact scenario. The premise is that you fire off a message to indicate that your collection has changed. Those interested ViewModels would then register to receive that message.
public CategoryAddedMessage
{
public Category AddedCategory { get; set; }
}
And then in your MainWindowViewModel
, you would need to fire off that message when appropriate
public AddCategory(Category newCategoryToAdd)
{
Categories.Add(newCategoryToAdd);
Messenger.Default.Send<CategoryAddedMessage>(new CategoryAddedMessage {AddedCategory = newCategoryToAdd});
}
..and then in your FilterWindowViewModel
, we need to register to receive this message and keep it's collection in sync:
public FilterWindowViewModel()
{
Categories = new ObservableCollection<Category>();
CategoryView = CollectionViewSource.GetDefaultView(Categories);
CategoryView.SortDescriptions.Add(new SortDescription("Type", ListSortDirection.Ascending));
Messenger.Default.Register<CategoryAddedMessage>(this, m => ReceiveCategoryAddedMessage(m));
}
and then your receiving method would be:
private void ReceiveCategoryAddedMessage(CategoryAddedMessage message)
{
Categories.Add(message.AddedCategory);
}
There are other techniques that can be used with MVVM Light, but hopefully this is enough to get you started and consider some approaches that may work for you.
Upvotes: 1