OneWayToSource binding is supplying default dependency property value (instead of the value that was set) when updating the data context
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
* .NET Core Version: 3.1, but I have also tried this in .NET 5 and the behaviour is the same.
* Windows version: Version 21H1 (OS Build 19043.1052)
* Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
**Problem description:**
When setting a value for a dependency property that is bound using `OneWayToSource`, the value being passed to the source is not that value but the default value for the dependency property.
**Actual behavior:** The value supplied to the view model property is the default value for the dependency property.
**Expected behavior:** The value supplied to the view model property is the value that was set for the dependency property.
**Minimal repro:**
[https://github.com/Metritutus/WpfOneWayToSourceIssue001](https://github.com/Metritutus/WpfOneWayToSourceIssue001)
There is no intended visual output for that program. It's intended to demonstrate the binding issue. To observe the issue, place a breakpoint in the setter for `ExampleViewModelProperty` in `HolderViewModel`, click the button, and observe the value of `value`. It comes through as `"DEFAULT"`, however I was expecting it to be `"Example Behavior Dependency Property"`.
I have also added Debug messages to help highlight the problem.
Although my main project is testing this with a `Behavior`, I have also added an attached property to the minimal repro project above to demonstrate that the issue occurs there as well. I use the Coerce callback to supply the value. If you want to test this, just comment out the `TextBlock` xaml with the `Behavior` and uncomment the `TextBlock` with the attached property.
This seems like a bug, but it is always possible I am doing something wrong. If this is not a bug, and I am also doing nothing wrong, please suggest what I can do to work around this issue as I have not yet found any way to get the value set in the dependency property to pass back to the source.
**EDIT:**
Following some further reading, I tried replacing the call to `SetValue()` in `OnAttached()` in [UIElementBehaviors.cs](https://github.com/Metritutus/WpfOneWayToSourceIssue001/blob/main/Behaviors/UIElementBehaviors.cs) with `SetCurrentValue()`, however this has had no effect on the issue.
Contributor guide
Assessment
This issue has not been assessed yet.