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

Tom Meier 240 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.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,594 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.
11,010 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,706 Reputation points
    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.) 44,011 Reputation points Microsoft Vendor
    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 Answers by the question author, which helps users to know the answer solved the author's problem.