private static void OnCanCloseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Window window = d as Window; if (window == null) { window = d.GetParent <Window>(); } if (window != null) { RoutedEventHandler loadedHandler = null; loadedHandler = delegate { if ((bool)e.NewValue) { WindowController.EnableClose(window); } else { WindowController.DisableClose(window); } window.Loaded -= loadedHandler; }; if (!window.IsLoaded) { window.Loaded -= loadedHandler; window.Loaded += loadedHandler; } else { loadedHandler(null, null); } } }