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.
goto window properties set
u wont get close buttons...
After much searching for the answer to this, I worked out this simple solution that I will share here in hopes it helps others.
I set
WindowStyle=0x10000000
.This sets the
WS_VISIBLE (0x10000000)
andWS_OVERLAPPED (0x0)
values for Window Style. "Overlapped" is the necessary value to show the title bar and window border. By removing theWS_MINIMIZEBOX (0x20000)
,WS_MAXIMIZEBOX (0x10000)
, andWS_SYSMENU (0x80000)
values from my style value, all the buttons from the title bar were removed, including the Close button.Let the user "close" the window but really just hide it.
In the window's OnClosing event, hide the window if already visible:
Each time the Background thread is to be executed, re-show background UI window:
When terminating execution of program, make sure all windows are/can-be closed:
WPF doesn't have a built-in property to hide the title bar's Close button, but you can do it with a few lines of P/Invoke.
First, add these declarations to your Window class:
Then put this code in the Window's Loaded event:
And there you go: no more Close button. You also won't have a window icon on the left side of the title bar, which means no System menu, even when you right-click the title bar -- they all go together.
Note that Alt+F4 will still close the Window. If you don't want to allow the window to close before the background thread is done, then you could also override OnClosing and set Cancel to true, as Gabe suggested.
The following is about disabling the close and Maximize/Minimize buttons, it does not actually remove the buttons (but it does remove the menu items!). The buttons on the title bar are drawn in a disabled/grayed state. (I'm not quite ready to take over all the functionality myself ^^)
This is slightly different than Virgoss solution in that it removes the menu items (and the trailing separator, if needed) instead of just disabling them. It differs from Joe Whites solution as it does not disable the entire system menu and so, in my case, I can keep around the Minimize button and icon.
The follow code also supports disabling the Maximize/Minimize buttons as, unlike the Close button, removing the entries from the menu does not cause the system to render the buttons "disabled" even though removing the menu entries does disable the functionality of the buttons.
It works for me. YMMV.
Usage: This must be done AFTER the source is initialized. A good place is to use the SourceInitialized event of the Window:
To disable the Alt+F4 functionality the easy method is just to wire up the Canceling event and use set a flag for when you really do want to close the window.
As stated in other answers, you can use
WindowStyle="None"
to remove the Title Bar altogether.And, as stated in the comments to those other answers, this prevents the window from being draggable so it is hard to move it from its initial position.
However, you can overcome this by adding a single line of code to the Constructor in the Window's Code Behind file:
Or, if you prefer Lambda Syntax:
This makes the entire Window draggable. Any interactive controls present in the Window, such as Buttons, will still work as normal and won't act as drag-handles for the Window.