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

Method OnPropertyChanged for exposed property is called with old value #511

Open
2 of 6 tasks
Green7 opened this issue Oct 3, 2023 · 5 comments
Open
2 of 6 tasks
Milestone

Comments

@Green7
Copy link

Green7 commented Oct 3, 2023

  • WPF
  • UWP
  • iOS
  • Android
  • .NET Standard
  • .NET Core

Component

ViewModel

Version of Library

Catel.MVVM 6.0.0-alpha1271
Catel.Fody 4.9.0

Version of OS(s) listed above with issue

Windows 11

Steps to Reproduce

  1. Create model:
public class AppSettingsModel : ViewModelBase
{
  public string SelectedThemeName { get; set; }
}
  1. Create ViewModel and OnPropertyChangedMethod:
public class AppSettingsViewModel : ViewModelBase
{
    [Model]
    [Expose("SelectedThemeName")]
    public AppSettingsModel AppSettings { get; set; }
   
    public AppSettingsViewModel(AppSettingsModel appSettings)
    {
       AppSettings = appSettings;
    }

    private void OnSelectedThemeNameChanged()
    {
       //  This method is called when AppSettings.SelectedThemeName have old value, not the current one
    }
}
  1. Change property (for example, by binding in the view)

Expected Behavior

OnPropertyChanged should be called after property gets its new value.

Actual Behavior

OnPropertyChanged is called before the property value changes.
But if we get the value of property using dynamic object it will be correct. For example:

    private void OnSelectedThemeNameChanged()
    {
      var oldValue = AppSettings.SelectedThemeName; // Old value 
      dynamic thisDynamic = this;
      var theme = thisDynamic.SelectedThemeName;  // Correct value.
    }

Also if we expose property as below:

public class AppSettingsViewModel : ViewModelBase
{
    [Model]
    public AppSettingsModel AppSettings { get; set; }

    [ViewModelToModel("AppSettings")]
    public string SelectedThemeName { get; set; }

OnSelectedThemeNameChanged works correctly and has the current value.

@GeertvanHorrik
Copy link
Member

GeertvanHorrik commented Oct 4, 2023

Thanks for reporting and the detailed error report. I will investigate this ASAP. Do you have a repo that I can use? E.g. do you have this code in a public repo?

@GeertvanHorrik GeertvanHorrik modified the milestones: 4.9.0, 4.10.0 Dec 29, 2023
@GeertvanHorrik
Copy link
Member

@GeertvanHorrik
Copy link
Member

@Green7
Copy link
Author

Green7 commented Dec 30, 2023

the problem occurs if the variable is bound in UI.
I used the code:
<TextBox Text="{Binding SelectedThemeName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

In such a situation, OnSelectedThemeNameChanged is called with the old value of AppSettings.SelectedThemeName.
But surprisingly, if we use the dynamic object (as in the example), it will show the new, correct value.

@GeertvanHorrik
Copy link
Member

Then it's probably the RaisePropertyChanged, I will try to reproduce this inside a unit test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants