views:

65

answers:

2

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.

+1  A: 

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;
    }
}

HTH,
Kent

Kent Boogaart
thanx man but it still doesnt solve my problem imagine i have two separte uswecontrols whom i want to comunicate with each other what is the mvvm pattern to solve this
yoav.str
It does because it's not the UserControls that should be communicating with each other, but their view models. And they can do that in the ways I mentioned.
Kent Boogaart
mmm... OK its a good idea , who r the MainViewModel(CustomersViewModel customers, CustomerPurchasesViewModel customerPurchases)arguments ? who send them ? how does they bind ?
yoav.str
Typically you would use dependency injection to provide them. But if you're just trying to get started, you could just construct them yourself when you create the `MainViewModel`.
Kent Boogaart
can you suggest on a wpf tutorial for beginers which is mvvm oriented please
yoav.str
A: 

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.

MarkB