I dont understand why OnPropertyChanged is not firing in neither my Model or ViewModel

Tom Meier 220 Reputation points
2024-07-09T21:32:59.66+00:00

I am using the MVVM Community Tollkit and below is the generated code. I also included my Model code and the OnPropertyChanged Method. I put a breakpoint in the generated code on the property Spousessn and it was fired and OnPropertyChanged in the generated code is only called if there is a value in spousessn and is called as I change the text in the bound control. but if there is no initial value for spousessn then OnPropertyChanged is never called nor is it called as i change the the text in the bound control. My issue here is why is the method OnPropertyChanged never fired in my Model Code.

namespace ModelLibrary
{
    partial class PYEModel
    {
        /// <inheritdoc cref="spousessn"/>
        [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        public string Spousessn
        {
            get => spousessn;
            set
            {
                if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(spousessn, value))
                {
                    OnSpousessnChanging(value);
                      OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Spousessn);
                    spousessn = value;
                    OnSpousessnChanged(value);
                    OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Spousessn);
                }
            }
        }
public partial class PYEModel:ObservableObject
{
	int ID { get; set; }
	[ObservableProperty]
	public string spousessn;
}
protected void OnPropertyChanged(string? propertyName = default)
{
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,369 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,818 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,081 Reputation points Microsoft Vendor
    2024-07-10T02:59:09.0766667+00:00

    Hello,

    If you want to re-write the OnPropertyChanged in the ObservableObject.

    This OnPropertyChanged(PropertyChangedEventArgs e) is virtual method, you need to add **override **in it and If you want to fire OnPropertyChanged method when value of property changed, you have to invoke the ObservableObject.OnPropertyChanged method by base.OnPropertyChanged(e);.

    You can refer to the following code.

    public partial class MyViewModel: ObservableObject
        {
     
    
            protected override void OnPropertyChanged(PropertyChangedEventArgs e)
            {
                ArgumentNullException.ThrowIfNull(e);
               base.OnPropertyChanged(e);
              
            }
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.