Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can achieve this by writing an <code>ICommand</code> that passes the <code>Window</code> instance in as a parameter.</p> <p>A good example is available here: <a href="https://stackoverflow.com/questions/3863431/how-can-i-assign-the-close-on-escape-key-press-behavior-to-all-wpf-windows-wit/4080162#4080162">How can I assign the &#39;Close on Escape-key press&#39; behavior to all WPF windows within a project?</a></p> <p>In that post the <code>ICommand</code> is eventually bound to a <code>KeyBinding</code> (so that the Escape key can be used to close the window) but you would be able to bind the command to any button or invoke it from anywhere within the view. The important part is to use a <code>RelativeSource</code> on your command parameter binding to reference the <code>Window</code> that you want to close</p> <p><strong>Edit in response to comments</strong></p> <p>The command is a singleton, but there is no requirement for it to be - it is only a singleton because it is stateless and it makes the binding easier. It acquires a reference to the <code>Window</code> through binding, so for a <code>UserControl</code> you can just use:</p> <pre><code>&lt;Button Command="{x:Static mynamespace:CloseWindowCommand.Instance}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Content="Close My Parent Window" /&gt; </code></pre> <p>To be able to call it from view model code is slightly more complicated and needs a different approach; a good example can be found here: <a href="http://gallery.expression.microsoft.com/WindowCloseBehavior" rel="nofollow noreferrer">http://gallery.expression.microsoft.com/WindowCloseBehavior</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload