MVVM event - how and where in MVVM architecture they should take place?
assuming i got this main window xmal:
<Window x:Class="MVVMTUTRIALS.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:TestMvvm444.Views"
Title="Window1" Height="300" Width="400" Loaded="Window_Loaded">
<Grid>
<views:CustomersList x:Name="CustomersList"/>
<views:CustomerBoughtList x:Name="CustomerBoughtList"/>
</Grid>
</Window>
and i want an event which take a rule in CustomersList (clicking on cerrtian raw) to invoke the CustomerBoughtList(show all this customer purchase ) to do somthing so my q are :
1.where should the event be 开发者_开发知识库? it's reasonable to think in the main window?
2.can someone please guide me what to do ?
i think the core of my misunderstanding is how does tow UserControl(s) comunicate with each other and with the view model
thanku foe reading and making notes.
There are various ways to tackle this. Here are a couple expressed in pseudo-code. Firstly, a coordinating view model:
public class CustomersViewModel : ViewModel
{
public event EventHandler<EventArgs> SelectedCustomerChanged;
public ICollection<Customer> Customers
{
get ...
}
public CustomerViewModel SelectedCustomer
{
get ...
set ...
}
}
public class CustomerPurchasesViewModel : ViewModel
{
public CustomerViewModel Customer
{
get ...
set ...
}
public ICollection<PurchaseViewModel> Purchases
{
get ...
}
}
public class MainViewModel : ViewModel
{
private CustomersViewModel customers;
private CustomerPurchasesViewModel customerPurchases;
public MainViewModel(CustomersViewModel customers, CustomerPurchasesViewModel customerPurchases)
{
this.customers = customers;
this.customerPurchases = customerPurchases;
// push changes in selection to the customer purchases VM
this.customers.SelectedCustomerChanged += delegate
{
this.customerPurchases.Customer = this.customers.SelectedCustomer;
};
}
}
Secondly, using mediator:
public class CustomersViewModel : ViewModel
{
public ICollection<Customer> Customers
{
get ...
}
public CustomerViewModel SelectedCustomer
{
get ...
set
{
...
eventHub.Publish(new CustomerSelectedMessage(value));
}
}
}
public class CustomerPurchasesViewModel : ViewModel, ISubscriber<CustomerSelectedMessage>
{
public CustomerViewModel Customer
{
get ...
set ...
}
public ICollection<PurchaseViewModel> Purchases
{
get ...
}
private void Receive(CustomerSelectedMessage m)
{
this.Customer = e.Customer;
}
}
public class MainViewModel : ViewModel
{
private CustomersViewModel customers;
private CustomerPurchasesViewModel customerPurchases;
public MainViewModel(CustomersViewModel customers, CustomerPurchasesViewModel customerPurchases)
{
this.customers = customers;
this.customerPurchases = customerPurchases;
}
}
You can have a view model for the main window that has two public list properties that your user controls can bind to. Then, when the selection is changed in the view, you can detect that in your view model list and do whatever you need to do with your other list. There would be no event handling in the view.
精彩评论