CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
2.8k stars 277 forks source link

Does not fire event OnPropertyChanged with enum property in constructor #891

Closed sonhai1710 closed 2 weeks ago

sonhai1710 commented 2 weeks ago

Describe the bug

I'm using CommunityToolkit.Mvvm 8.2.2 and .NET 8.0. I created an Enum property: `

public enum MyEnum { First, Second }
public partial class MainWindowViewModel : ObservableObject
{
    [ObservableProperty]
    MyEnum testEnum;

    partial void OnTestEnumChanged(MyEnum value)
    {
        MessageBox.Show(value.ToString());
    }

    [RelayCommand]
    private void Click()
    {
        TestEnum = MyEnum.Second;
    }
    public MainWindowViewModel()
    {
        TestEnum = MyEnum.First;
    }
}

` But it does not fire the event OnTestEnumChanged. It works fine if I change the value of TestEnum in command ClickCommand For the string value or other types, it works fine, except for enum type. How can I fire the OnTestEnumChanged in constructor?

Regression

No response

Steps to reproduce

1. Add break point to event OnTestEnumChanged
2. Run the Debug in visual studio
3. It loads the window without going to the breakpoint

Expected behavior

It will go to the OnTestEnumChanged before window loaded, because I set the value of TestEnum in constructor

Screenshots

No response

IDE and version

VS 2022

IDE version

No response

Nuget packages

Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

Sergio0694 commented 2 weeks ago

This is the expected behavior. You're assigning a value to the property that it already has, so there is no change, hence why the method isn't invoked. I'm not even sure how you'd imagine actually fixing this (other than adding some additional state to track "is this the first time the setter is invoked" or something).