In my new project, which I'm developing with WPF in .NET 10,
I'm having trouble using the PasswordBox in a user authentication window.
In my code, I've used the PasswordBox's KeyDown and PreviewKeyDown events.
Normally, when a user enters their password and presses Enter, if the password is correct, DialogResult is set to true and the Close() method is called.
And it's at this point that the program stops, when DialogResult is set to true.
The program crashes without returning an error message.
However, in a previous project I developed with WPF in .NET Framework,
I didn't encounter this problem. Despite my research, I haven't found a solution.
The good news is that when the validation button is clicked after entering the password, the window closes without any problem. Pressing Enter when the PasswordBox has focus causes a problem.
Please, I need to understand how to fix this bug.

Raghunath BhukanPosted Jan 8, 2026, 5:50 AM
This is a real WPF behavior change / pitfall.
The key point is:
What’s actually going wrong
1.
DialogResultin WPF is not passiveIn WPF:
does three things at once:
Sets the result
Calls
Close()Ends the modal message loop (
ShowDialog())So this:
is already dangerous (double-close).
2. Why it crashes specifically on Enter in
PasswordBoxPasswordBoxis special:It handles keyboard input internally
Enteris processed before WPF finishes its input pipelineClosing the window inside
KeyDown/PreviewKeyDowninterrupts that pipelineIn newer .NET / WPF builds (including .NET 8+ / .NET 10):
WPF is stricter
This results in an unhandled dispatcher exception
Often appears as a silent crash if not observed in the Output window
That’s why:
? Clicking the button works
? Pressing Enter crashes
3. Why it worked in .NET Framework
Older WPF versions were more forgiving:
Re-entrancy issues were swallowed
Input pipeline teardown was less strict
.NET Core / .NET 5+ WPF fixed (or exposed) this behavior.
? What NOT to do
or even:
? Correct & SAFE solutions
? Solution 1 (BEST PRACTICE): Use a default button (
IsDefault)This is the WPF equivalent of WinForms
AcceptButton.? No KeyDown handling
? No crashes
? Native WPF behavior
? Accessible & clean
? Solution 2: Use
Dispatcherto defer closingIf you must handle Enter manually:
Why this works:
Lets WPF finish processing the key event
Closes the dialog after input handling completes
? Solution 3: Don’t set
DialogResultin KeyDown at allStill relies on the button logic, not raw dialog manipulation.
Important rules to remember
Rule 1
Rule 2
Otherwise WPF throws internally.
Rule 3
(
KeyDown,PreviewKeyDown,TextInput)Use commands or buttons instead.
? Final recommendation
For authentication dialogs in WPF (.NET 10):
? Use
PasswordBox? Use a Login button with
IsDefault="True"? Set
DialogResultonly in the button click? Do NOT handle Enter manually unless absolutely necessary