C# Binding CommandBinding.执行到ViewModel中的方法/事件处理程序
我只是在寻找一个很好的解决方案来绑定应用程序。将命令保存到我的viewModel,并考虑了以下问题:C# Binding CommandBinding.执行到ViewModel中的方法/事件处理程序,c#,wpf,xaml,mvvm,data-binding,C#,Wpf,Xaml,Mvvm,Data Binding,我只是在寻找一个很好的解决方案来绑定应用程序。将命令保存到我的viewModel,并考虑了以下问题: <Window.CommandBindings> <CommandBinding Command="Save" Executed="{Binding SaveCommandAction}"/> </Window.CommandBindings> 结果=>它不工作。。。我不知道为什么。您将得到一个InvalidCastException,它说他不能将
<Window.CommandBindings>
<CommandBinding Command="Save" Executed="{Binding SaveCommandAction}"/>
</Window.CommandBindings>
结果=>它不工作。。。我不知道为什么。您将得到一个InvalidCastException,它说他不能将“System.Reflection.RuntimeEventInfo”转换为“System.Reflection.MethodInfo”。。。
CommandBinding.Executed属性的类型为ExecutedRoutedEventHandler
这里有人知道是什么样的WPF魔法引发了这个例外吗?
我知道这个解决方案有点脏,但它实际上是一个非常简单的解决方案。为什么不想将VM命令绑定到这个绑定?因为我有很多ViewModels都在使用Application.Save命令,但它们都应该在MainViewModel中处理,MainViewModel是上面显示的窗口的DataContext。我知道有很多方法可以解决这个问题,但我实际上想知道为什么这种方法不起作用^^为什么不想将VM命令绑定到此绑定?因为我有很多ViewModel都使用Application.Save命令,但它们都应该在MainViewModel中处理,MainViewModel是上面显示的窗口的DataContext。我知道有很多方法可以解决这个问题,但我想知道为什么这种方法不起作用^^
public ExecutedRoutedEventHandler SaveCommandAction => Save;
public void Save(object sender, ExecutedRoutedEventArgs e)
{
//Do Something...
}