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
中處理這個問題,但我不想。我希望它在視圖模型中處理。
什麼是正確的語法?只要我達到同樣的行爲,我願意接受任何其他想法。