The easiest way is to implement ButtonClick
event handler and invoke Window.Close()
method, but how doing this through a Command
binding?
相关问题
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
- How to properly change a resource dictionary
I think that in real world scenarios a simple click handler is probably better than over-complicated command-based systems but you can do something like that:
using RelayCommand from this article http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
The simplest solution that I know of is to set the
IsCancel
property to true of the closeButton
:No bindings needed, WPF will do that for you automatically!
Reference: MSDN Button.IsCancel property.
Actually, it is possible without C# code. The key is to use interactions:
In order for this to work, just set the
x:Name
of your window to "window", and add these two namespaces:This requires that you add the Expression Blend SDK DLL to your project, specifically
Microsoft.Expression.Interactions
.In case you don't have Blend, the SDK can be downloaded here.
For .NET 4.5 SystemCommands class will do the trick (.NET 4.0 users can use WPF Shell Extension google - Microsoft.Windows.Shell or Nicholas Solution).
In the Code Behind you can implement the handlers like this:
All it takes is a bit of XAML...
And a bit of C#...
(adapted from this MSDN article)
One option that I've found to work is to set this function up as a Behavior.
The Behavior:
On the XAML Window, you set up a reference to it and bind the Behavior's Close property to a Boolean "Close" property on your ViewModel:
So, from the View assign an ICommand to change the Close property on the ViewModel which is bound to the Behavior's Close property. When the PropertyChanged event is fired the Behavior fires the OnCloseTriggerChanged event and closes the AssociatedObject... which is the Window.