Calvin
Calvin

Reputation: 641

How can I call a WPF command from a Winform's Button?

I'm using both winform and incorporating wpf through ElementHost.

How can I call a WPF ICommand from a Winforms button's click event? These are all new to me so bear with me with these kind of questions.

My current code is

CarView car = (CarView) CarHost.Child;         
CarViewModel cvm = (CarViewModel) car.DataContext;
cvm.SaveCommand.Execute(null);

So by doing that I was able to call the SaveCommand, but I dont get any data.

Thanks in advance.

Upvotes: 0

Views: 1272

Answers (1)

Tim
Tim

Reputation: 15237

I might be missing something here. Normally you'd do something like:

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

And then when someone clicks the button, MyCommand's Execute method is called.

I suppose, from a codebehind, you could call:

private void OnClick(object sender, RoutedEventArgs e)
{
    if (MyCommand.CanExecute(null))
        MyCommand.Execute(null);
}

But except for very specific circumstances (which you haven't mentioned) I'm not sure why you'd do it that way. I think you definitely need to give us a little more information.

Upvotes: 2

Related Questions