locked
[SOLVED] UpdateSourceTrigger=PropertyChanged doesn't work

    Question

  • I'm going crazy. A XAML textbox element is correctly binding to a property, but the UpdateSourceTrigger=OnPropertyChanged is being ignored. Here's the weird thing....it was working before. Then I made some code changes, but reverted back using git. What's even weirder is that the binding would raise a property change when the textbox lost focus, but after the revert, now it doesn't even raise a property change when the textbox loses focus.

    Any suggestions as to why it may not be working? I don't understand how even losing focus no longer raises a property change. I'm using MVVM light toolkit.

    <TextBox x:Name="userAccessFirstNameField" Style="{StaticResource FlyoutField}" Text="{Binding NewUser.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>


    public AccessViewModel() { SaveNewUserCommand = new RelayCommand<Popup>( (popup) => SaveNewUser(popup), (popup) => CanSaveNewUser(popup)); CreateNewUser(); } private void CreateNewUser() { if (NewUser != null) NewUser.PropertyChanged -= NewUser_PropertyChanged; NewUser = new UserAccount(); NewUser.PropertyChanged += NewUser_PropertyChanged; // clear the password fields for new account Password = string.Empty; PasswordConfirm = string.Empty; // clear any access privileges set except Register privilege NewUser.Register = true; NewUser.Reports = false; NewUser.Programming = false; NewUser.Access = false; NewUser.Settings = false; NewUser.NoSale = false; } private void NewUser_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "FirstName" || e.PropertyName == "LastName" || e.PropertyName == "Register" || e.PropertyName == "Reports" || e.PropertyName == "Programming" || e.PropertyName == "Access" || e.PropertyName == "Settings" || e.PropertyName == "NoSale") SaveNewUserCommand.RaiseCanExecuteChanged(); }

    private UserAccount _newUser;
            public UserAccount NewUser
            {
                get { return _newUser; }
                set { Set<UserAccount>(() => NewUser, ref _newUser, value); }
            }

    public class UserAccount : ObservableObject
        {
            public UserAccount()
            {
                register = true;
            }
    
            private string firstName;
            public string FirstName
            {
                get { return firstName; }
                set 
                {
                    firstName = value;
                    firstName = firstName.Trim();
                    Set<string>(() => FirstName, ref firstName, value); 
                }
            }
    .......



    • Edited by Qoheleth07 Thursday, April 23, 2015 7:11 AM
    Thursday, April 23, 2015 12:41 AM

Answers

  • I found out the reason is that I was setting the backing field for a property to value, making them the same value, preventing RaisePropertyChanged from being called inside Set<>().
    Thursday, April 23, 2015 7:13 AM

All replies

  • I found out the reason is that I was setting the backing field for a property to value, making them the same value, preventing RaisePropertyChanged from being called inside Set<>().
    Thursday, April 23, 2015 7:13 AM
  • Hi Qoheleth07,

    Thank you very much for sharing the solution to us.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 23, 2015 9:03 AM
    Moderator