Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visual Studio 2022 v17.12 does not update Binding to [ObservableProperty] #10250

Open
bjorn-malmo opened this issue Dec 20, 2024 · 0 comments
Open
Labels
bug Something isn't working team-Markup Issue for the Markup team

Comments

@bjorn-malmo
Copy link

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 (dotnet/roslyn#76470)
Issue reported in Community Toolkit can reproduce, but closed as external (CommunityToolkit/dotnet#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";
    }
}
  1. In the control's template, bind to the property
    <Style TargetType="local:CustomControl1" >
        <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>
    </Style>
  1. 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:

Image

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

Image

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

@bjorn-malmo bjorn-malmo added the bug Something isn't working label Dec 20, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Dec 20, 2024
@karkarl karkarl added team-Markup Issue for the Markup team and removed needs-triage Issue needs to be triaged by the area owners labels Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working team-Markup Issue for the Markup team
Projects
None yet
Development

No branches or pull requests

2 participants