microsoft / microsoft/microsoft-ui-xaml
Visual Studio 2022 v17.12 does not update Binding to [ObservableProperty]
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
### Describe the bug
A WinUI custom control decorated with [ObservableObject] attribute will not update Bindings in the control's template for properties on property changed.
**This does work in Visual Studio 2022 v17.10!**
**This does NOT work in Visual Studio 2022 v17.12!**
Roslyn gang directed me here (https://github.com/dotnet/roslyn/issues/76470)
Issue reported in Community Toolkit can reproduce, but closed as external (https://github.com/CommunityToolkit/dotnet/issues/1011)
### Steps to reproduce the bug
1. Create a WinUI 3 Custom Control
2. Using CommunityToolkit, attribute the class [ObservableObject]
3. Create a field and attribute it [ObservableProperty]
```
[ObservableObject]
public sealed partial class CustomControl1 : Control
{
public CustomControl1()
{
this.DefaultStyleKey = typeof(CustomControl1);
}
[ObservableProperty] string _myValue = "Initial value";
[RelayCommand]
void UpdateValue()
{
MyValue = "Updated value";
}
}
```
5. In the control's template, bind to the property
```
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomControl1">
<StackPanel Spacing="16"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<TextBlock Text="{Binding MyValue, RelativeSource={RelativeSource TemplatedParent}}"/>
<Button Content="Update value!"
Command="{Binding UpdateValueCommand, RelativeSource={RelativeSource TemplatedParent}}"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
```
7. Update the value of the MyValue property.
Sample application found here: Project PropertyBindingTest.sln in repo https://github.com/bjorn-malmo/bug-free-happiness/
Run the application using Visual Studio 2022 v17.10 and click the button:

Run the same application using Visual Studio 2022 v17.12 and click the button:

### Expected behavior
The Binding to update as a result of the change notification
### Screenshots
_No response_
### NuGet package version
WinUI 3 - Windows App SDK 1.6.3: 1.6.241114003
### Windows version
Windows 11 (23H2): Build 22631
### Additional context
_No response_
Contributor guide
Research direction
Start with the PropertyBindingTest.sln sample application linked in the issue and reproduce the binding behavior under Visual Studio 2022 v17.10 and v17.12. Compare the [ObservableObject], [ObservableProperty], and [RelayCommand] custom-control setup, and verify that the templated-parent binding updates after clicking the button.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop, frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100