I'm writing a modal dialog in WPF. How do I set a WPF window to not have a close button? I'd still like for its WindowState to have a normal title bar.
I found ResizeMode, WindowState, and WindowStyle, but none of those properties allow me to hide the close button but show the title bar, as in modal dialogs.
To disable close button you should add the following code to your Window class (the code was taken from here, edited and reformatted a bit):
This code also disables close item in System menu and disallows closing the dialog using Alt+F4.
You will probably want to close the window programmatically. Just calling
Close()
will not work. Do something like this:XAML Code
should work
Edit- for your instant this Thread shows how that can be done but I don't think Window has a property to get what you want without losing the normal title bar.
Edit 2 This Thread shows a way for it to be done, but you must apply your own style to the system menu and it shows a way how you can do that.
Try adding a Closing event to the window. Add this code to the event handler.
This will prevent the window from closing. This has the same effect as hiding the close button.
Use this, modified from https://stephenhaunts.com/2014/09/25/remove-the-close-button-from-a-wpf-window :
If the need is only to prohibit the user from closing the window, this is a simple solution.
XAML code:
IsCloseButtonEnabled="False"
I was trying Viachaslau's answer since I like the idea of not removing the button but disabling it, but for some reason it did not always work: the close button was still enabled but no errors whatsoever.
This on the other hand always worked (error checking omitted):