2016-12-27 37 views
1

在Commands.cs視圖模型處理它,一個命令被定義:從一個用戶控制觸發的命令和在另一

public static readonly RoutedUICommand Account_OpenDetails 
      = new RoutedUICommand("show account details", 
            "Account_OpenDetails", 
            typeof(MainWindow)); 

UserControl1View我有:

<i:Interaction.Triggers> 
     <i:EventTrigger EventName="MouseLeftButtonDown"> 
      <i:InvokeCommandAction Command="LocalCommands:Command.Account_OpenDetails" 
            CommandParameter="{Binding AccountId}" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 

UserControl2View我想這樣做:

<UserControl.CommandBindings> 
     <CommandBinding Command="LocalCommands:Command.Account_OpenDetails" 
         Executed="{Binding Account_OpenDetails}" /> 
    </UserControl.CommandBindings> 

而且我希望這會調用下面的方法d在UserControl2ViewModel(這是UserControl2View DataContext的):

public void Account_OpenDetails(object sender, ExecutedRoutedEventArgs e) 

相反,我得到:

無法投類型的對象System.Reflection.RuntimeEventInfo「到 型「系統。 Reflection.MethodInfo」。

基本上,從一個用戶控件的XAML我想觸發一個命令,這是在另一個用戶控件的視圖模型中處理的。我可以在UserControl2View.cs中處理這個問題,但我不想。我希望它在視圖模型中處理。

什麼是正確的語法?只要我達到同樣的行爲,我願意接受任何其他想法。

回答

0

基本上,從一個用戶控制的XAML我想觸發一個命令,其在另一用戶控制

在主視圖模型創建共享布爾標誌的視圖模型處理這會標記第二個控件。按着這些次序。

  1. 在主視圖模型上創建一個通知Boolean標誌(如IsAccountDetailsOpen)。
  2. 使用輔助控件上的更改處理程序創建Boolean依賴項屬性。在更改處理程序方法中,根據您提到的控件的視圖模型執行需要完成的操作。
  3. 在主頁上,將依賴項屬性綁定到第二個控件的IsAccountDetailsOpen
  4. 在命令代碼中,當事件發生時,將布爾標誌IsAccountDetailsOpen更改爲true發信號通知第二個控件。
  5. -0r-使用雙向綁定在第一個控件上創建一個布爾依賴關係,並將IsAccountDetailsOpen設置爲true,以通知另一個控件。
相關問題