Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>just try this</p> <p>Create Close Behavior like this</p> <pre><code>public static class WindowCloseBehavior { public static readonly DependencyProperty IsOpenProperty = DependencyProperty.RegisterAttached("IsOpen", typeof(bool), typeof(WindowCloseBehavior), new PropertyMetadata(IsOpenChanged)); private static void IsOpenChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { Window window = Window.GetWindow(obj); if (window != null &amp;&amp; ((bool)args.NewValue)) window.Close(); } public static bool GetIsOpen(Window target) { return (bool)target.GetValue(IsOpenProperty); } public static void SetIsOpen(Window target, bool value) { target.SetValue(IsOpenProperty, value); } } </code></pre> <p>Create a property in View Model let it be</p> <pre><code> private bool closeMe=false ; public bool CloseMe { get { return closeMe; } set { closeMe = value; RaisePropertyChanged("CloseMe"); } } </code></pre> <p>and in the View just bind the CloseMe value into the Behavior . When you click on the Button make the CloseMe = True and this will close the current window</p> <p>eg </p> <pre><code>&lt;Window x:Class="WpfApplication12.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Behavior="clr-namespace:WpfApplication12" Behavior:WindowCloseBehavior.IsOpen="{Binding CloseMe}" Title="Window1" Height="300" Width="300"&gt; &lt;Grid&gt; &lt;CheckBox IsChecked="{Binding CloseMe}" Content="Close" Margin="5"&gt;&lt;/CheckBox&gt; &lt;/Grid&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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