Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Winform';调用WPF命令;s按钮?_C#_Wpf_Winforms - Fatal编程技术网

C# 如何从Winform';调用WPF命令;s按钮?

C# 如何从Winform';调用WPF命令;s按钮?,c#,wpf,winforms,C#,Wpf,Winforms,我通过ElementHost使用winform和合并wpf 如何从Winforms按钮的单击事件调用WPFICommand?这些对我来说都是新的,所以请容忍我提出这些问题 我现在的代码是 CarView car = (CarView) CarHost.Child; CarViewModel cvm = (CarViewModel) car.DataContext; cvm.SaveCommand.Execute(null); 因此,通过这样做,我可以调用SaveCommand

我通过
ElementHost
使用winform和合并wpf

如何从Winforms按钮的单击事件调用WPF
ICommand
?这些对我来说都是新的,所以请容忍我提出这些问题

我现在的代码是

CarView car = (CarView) CarHost.Child;         
CarViewModel cvm = (CarViewModel) car.DataContext;
cvm.SaveCommand.Execute(null);
因此,通过这样做,我可以调用
SaveCommand
,但我没有得到任何数据


提前谢谢。

我可能遗漏了什么。通常情况下,您会这样做:

<Button Command="{Binding MyCommand}" />

但除了非常具体的情况(你没有提到)之外,我不知道你为什么会这样做。我想你肯定需要给我们提供更多的信息。

一旦你有了ICommand实例,你就可以调用它的Execute,或者如果你想要CanExecute,就调用Execute。@Adam Mills——我没有听你的。你能给我举个例子吗?为什么不直接使用按钮的
命令
属性而不是使用
单击
事件?@Rachel——因为我没有任何按钮的属性调用命令。请记住,我要调用ICommand的按钮不在xaml中。它只是一个普通的按钮。我再次使用elementhost显示xaml。@Rachel,因为他在本节中使用WinForms,但试图从托管WPF控件访问ICommands。他在下面回答我的问题时提到了这一点。我正在使用winform并通过elementhost合并wpf。通过这样做,我想从代码隐藏的click事件中调用icommand。好吧,那么,您可能想执行我在C代码中显示的操作。我传入null,因为我假设没有参数,但您也可以传入参数。
private void OnClick(object sender, RoutedEventArgs e)
{
    if (MyCommand.CanExecute(null))
        MyCommand.Execute(null);
}