2014-06-12 26 views
0

在WPF中我試圖儘可能地作爲MVC/VM。我有一個用戶控件(ReservationDataControl),它有一個帶有上下文菜單和一些過濾按鈕的數據網格。用戶可以從上下文菜單中添加,刪除等,它們都在usercontrol內處理。此控件與另一個用戶控件位於同一窗口中,我收集有關已付費人數和已賺多少的統計信息(PartyStatsControl)。我的用戶控件應該如何通知我的視圖?

當用戶編輯ReservationDataControl時,我需要PartyStatsControl進行更新(所以它們將是有效的)。什麼是最好的MVC/VM方式來做到這一點?

我應該從ReservationDataControl引發一個事件嗎?有沒有一種綁定命令的方法?什麼是控制通知它的主機在MVC/VM的變化的可接受的方式。

順便說一句:我使用MVC /虛擬機,因爲我從來沒有設法得到我的腦海中的兩個直的差異,而不是爭論與我使用的方法我只是警察不知道馬上蝙蝠。

+0

這只是什麼樣的事件聚合被造的。您也可以使用rx公開服務,具體取決於您的設計。 –

+0

單個事件的事件和EventAggregator之間有什麼區別? –

+0

一個是分離的,另一個不是。 –

回答

0

與大多數事情的工程,有沒有正確錯誤的答案,而這一切都取決於具體的方案。在後面的代碼中處理事件並適當地調用視圖模型非常好(如果實例化時您已將視圖模型的引用傳遞到視圖中)。

但是,您可以綁定到命令,我更喜歡這種方法,因爲它更鬆散耦合。此命令中的操作將設置您的型號(在此情況下,創建您的ReservationDataViewModel時傳入此實例)。

ReservationDataControl查看:

<Button Command={Binding ClickCommand}/> 

ReservationDataControl視圖模型(數據上下文):現在

private readonly Statistics _statistics; 

public ICommand ClickCommand { get; set; } 

private ReservationDataViewModel(Statistics statistics) 
{ 
    _statistics = statistics; 

    InitialiseCommands(); 
} 

private void InitialiseCommands() 
{ 
    ClickCommand = new RoutedUICommand("ClickCommand", "ClickCommand", typeof(ViewModel)); 
    CommandBinding clickCommandBinding = new CommandBinding(ClickCommand, ExecuteClickCommand, ClickCommandCanExecute); 
    CommandManager.RegisterClassCommandBinding(typeof(ViewModel), clickCommandBinding); 
} 

private void ExecuteClickCommand(object sender, ExecutedRoutedEventArgs args) 
{ 
    // perform click logic here... 

    // e.g. say there is a property on your statistics called 'ReservationCount' 
    // the command adds a reservation (increments the count) 
    _statistics.ReservationCount += 1; 
} 

private void ClickCommandCanExecute(object sender, CanExecuteRoutedEventArgs args) 
{ 
    args.CanExecute = true; // put your can execute logic here... 
} 

,如果你的模型(即統計類)設置正確,它應該有一個告訴任何關心它已經改變的代碼的方式。

public class Statistics 
{ 
    public event EventHandler ReservationCountChanged; 

    private int _reservationCount; 

    // think about how you may restrict set access for this model 
    public int ReservationCount 
    { 
     get { return _reservationCount; } 
     protected set 
     { 
      if (_reservationCount != value) 
      { 
       _reservationCount = value; 

       if (ReservationCountChanged != null) 
        ReservationCountChanged(this, new EventArgs()); 
      } 
     } 
    } 

    public Statistics() { } 
} 

現在,您PartyStatsViewModel可以在這個變化回暖,源於ReservationDataViewModel和更新的觀點與標準的WPF綁定實現。

PartyStatsControl視圖模型:

private readonly Statistics _statistics; 

public int ReservationCount 
{ 
    get { return _statistics.ReservationCount; } 
    set 
    { 
     _statistics.ReservationCount = value; 

     OnPropertyChanged("ReservationCount"); 
    } 
}    

public PartyStatsControlViewModel(Statistics statistics) 
{ 
    _statistics = statistics; 

    _statistics.ReservationCountChanged += OnReservationCountChanged; 
} 

private void OnReservationCountChanged(object sender, RoutedEventArgs args) 
{ 
    // todo: force this onto the UI thread or exceptions will occur 
    this.ReservationCount = ((Statistics)sender).ReservationCount; 
} 
+0

因此,在完成自己的處理後,在我的UserControl上擁有一個依賴屬性會更合適,該屬性會調用RoutedCommand?然後視圖可以通過調用第二個控件上的刷新來響應該命令? –

+0

好吧,這聽起來像我可能誤解了最初的要求。你有一個用戶控件會改變一些數據,你想要一些第二控件來自動反映這些數據的狀態? –

+0

是的!這是主旨。 –

相關問題