dotnet / dotnet/wpf

When window closed with DataGrid cell is in edit mode, active edits are not committed

Open
#9,874 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

The in WPF general default behavior for an editable control that supports text input is to push binding update to source when the control loses focus.
When the user closes the active window and focused elements automatically loose focus and this causes any edits on the focused control to be pushed before the window closes.
However with the DataGrid control, if the window is closed and there is a text cell in edit mode it doesn't cause the binding to push the cell contents.

This issue seems to be due to the cell control retaining keyboard focus after it has lost logical focus. Which would make since if the cell was composed of multiple separate controls which could lose and gain focus while the cell was still being edited.
See [DataGridCell.OnAnyLostFocus](https://github.com/dotnet/wpf/blob/0d959af10519bdb23c19cd435611f10bdebe78bf/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridCell.cs#L553)
However when logical focus leaves the visual hierarchy of the current cell, then the current cell selection should be cleared.

### Reproduction Steps

1. Create a window with a DataGrid element
2. Bind the DataGrid to a collection of items

While running program
1. Begin editing DataGrid
2. Change cell value
3. Close window

Attached simple application for reproducing issue: [WpfDataGridAppClosingIssue.zip](https://github.com/user-attachments/files/17216658/WpfDataGridAppClosingIssue.zip)
cell

### Expected behavior

When app closes current edits are committed

### Actual behavior

cell edits are not committed when window closes

### Regression?

_No response_

### Known Workarounds

Set Binding.UpdateSourceTrigger to UpdateSourceTrigger.LostFocus for each editable data grid column.
This workaround method does have some side effects: that I'm aware of, cells always push binding when cell lose focus rather than when row exited.

### Impact

_No response_

### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.