Professor Chaos
Professor Chaos

Reputation: 9070

Bind window closing event to an ICommand

I have a simple WPF application where I don't have any mvvm framework like mvvmlight. Is there as way to bind a window close event to an ICommand that's in the view model?

I tried something like

<Window.CommandBindings>
    <CommandBinding Command="Close" Executed="{Binding WindowClose}" />
</Window.CommandBindings>

but obviously it doesn't work because it's looking for an event handler. Is there a simple way to do this?

Upvotes: 2

Views: 1206

Answers (1)

Rover
Rover

Reputation: 2230

Subscribe on event Closing and invoke WindowClose.Execute(null) from code. Or use EventToCommand (from some MVVM toolkits).

Upvotes: 2

Related Questions