wpf
wpf copied to clipboard
Timing problem in WPF data binding in styles
Description
I wanted to have data validation at once in WPF. At the same time I wanted to detect LostFocus, this is the time I modify the DB. Moreover, I needed this in a DataGrid
. I introduced a temp property. I use the PropertyChanged
at the TempName
so the validation happens at once and used the event handler at LostFocus
. The event handler copied the TempName
to the Name
and triggered the DB change. This method worked.
I wanted to use Behavior instead of event handler.
FinalizeNameCommand
copies the TempName
to the Name
. Visual Studio says there is a Binding problem at FinalizeNameCommand
though the binding works.
The binding error says the DataContext
is null, it should be the object behind the selected row, Person
. FinalizeNameCommand
is in that object and it is executed as expected. It seems the Binding is evaulated before the DataContext
is set, that is why an error is shown. Later the DataContext
is set and the binding is reevaulated (?), that is why FinalizeNameCommand
works.
The problem is not only a wrong error message. This causes other problems in the code later.
Reproduction Steps
https://www.dropbox.com/scl/fi/pugbf1mxo72yjv64sqt8x/ImmediateValidationBindingTest.zip?rlkey=cflx26ayz6kusz3vino1mf90l&dl=0
Download the solution.
Run the program
See the binding error
Modify a name and see that FinalizeNameCommand
is executed
Expected behavior
There should be no binding error
Actual behavior
There is binding error
Regression?
No response
Known Workarounds
use events instead of behavior
Impact
No response
Configuration
.net8 windows x64
Other information
No response