I am using the MVVM pattern and I want to get the value of the property in the OnPropertyChanged event

Tom Meier 260 Reputation points
2024-07-10T16:20:54.3866667+00:00

I want to get the value of the property in the OnPropertyChanged event. I would rather not use reflection.

Developer technologies | .NET | .NET MAUI
Developer technologies | C#
Developer technologies | 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.
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 81,616 Reputation points Volunteer Moderator
    2024-07-10T21:21:10.8366667+00:00

    The OnPropertyChanged() method would need to use reflection to get the value. also the passed name is just specified in an attribute, it does not specify an actual underlying property (or object if nested).

    0 comments No comments

  2. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,151 Reputation points Microsoft External Staff
    2024-07-12T02:52:33.5933333+00:00

    Hello,

    It is unfeasible to get the value in the PropertyChanged event. The reason is as follows.

    In the OnPropertyChanged event, it serves to notify other components that the value of this property has changed. Therefore, in this event you will only find the property PropertyName and not the value. when using this method, it is common to manually assign the value in the setter method and then call this method. You could refer to the following documentation.

    Best Regards,

    Alec Liu.


    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.

    0 comments No comments

Your answer

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