2010-08-22 43 views
1

假設我得到這個主窗口Xmal位:MVVM事件 - 他們應該如何在MVVM體系結構中發生?

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

,我想這需要在CustomersList規則的事件(上cerrtian原料點擊)來調用 的CustomerBoughtList(顯示所有該客戶購買)做財產以後這樣我的q是:

1.事件應該在哪裏?在主窗口中思考是合理的?

2.can某人請指導我該怎麼做?

我想我誤解的核心是如何拖用戶控件(S)相互之間以及與視圖模型

thanku敵人的閱讀和做筆記comunicate。

回答

1

有多種方法可以解決這個問題。這裏有一對用僞代碼表示。首先,協調視圖模型:

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

其次,利用中介:

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

thanx man但它仍然不能解決我的問題想象我有兩個sevise uswecontrols我想互相交流什麼是mvvm模式來解決這個問題 – 2010-08-22 21:39:37

+0

它確實是因爲它不是UserControl應該彼此通信,但他們的觀點模型。他們可以用我提到的方式做到這一點。 – 2010-08-22 22:19:55

+0

mmm ...確定它是一個好主意,誰是MainViewModel(CustomersViewModel客戶,CustomerPurchasesViewModel客戶購買) 參數?誰發送它們?他們如何綁定? – 2010-08-23 10:56:17

-1

您可以爲有您的用戶控件可以綁定到兩個公共列表屬性主窗口視圖模型。然後,當視圖中的選擇被更改時,您可以在視圖模型列表中檢測到該選擇,並執行您需要處理的其他列表。視圖中不會有事件處理。