How can I RaisePropertyChanged on property change?

55,540

Solution 1

PropertyChanged is used to notify the UI that something has been changed in the Model. Since you're changing an inner property of the User object - the User property itself is not changed and therefore the PropertyChanged event isn't raised.

Second - your Model should implement the INotifyPropertyChanged interface. - In other words make sure UserAccount implements INotifyPropertyChanged, otherwise changing the firstname will not affect the view either.

Another thing:

The parameter RaisePropertyChanged should receive is the Name of the property that has changed. So in your case:

Change:
RaisePropertyChanged(String.Empty);

To
RaisePropertyChanged("User");

From MSDN:

The PropertyChanged event can indicate all properties on the object have changed by using either null or String.Empty as the property name in the PropertyChangedEventArgs.

(No need to refresh all the Properties in this case)

You can read more on the concept of PropertyChanged here

Solution 2

You can invoke a property changed event from another class. Not particularly useful if you have all the sources. For closed source it might be. Though I consider it experimental and not production ready.

See this console copy paste example:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    public class Program
    {
        static void Main(string[] args)
        {
            var a = new A();
            a.PropertyChanged += A_PropertyChanged;
            var excpl = new Excpl();
            excpl.Victim = a;
            excpl.Ghost.Do();
        }

        private static void A_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Console.WriteLine("Event triggered");
        }
    }

    [StructLayout(LayoutKind.Explicit)]
    public struct Excpl
    {
        [FieldOffset(0)]
        public A Victim;
        [FieldOffset(0)]
        public C Ghost;
    }

    public class A : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class C : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void Do()
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(""));
        }
    }
}
Share:
55,540
A.T.
Author by

A.T.

Wannabe To offer yourself to someone for smiles, To share someone’s grief, To have love in your heart for someone, This is what life is all about. Granted, we are poor, by the standard of our pockets. Even so, we are rich at heart.

Updated on January 27, 2021

Comments

  • A.T.
    A.T. over 3 years

    Here I added a model to my viewmodel:

    public dal.UserAccount User  {
      get
      {
        return _user;
      }
      set
      {
        _user = value;
        RaisePropertyChanged(String.Empty); 
       }
    }
                    
    

    I handle property change event...

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
      if (this.PropertyChanged != null)
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
            
            
    

    This is the binding I use:

    <TextBox Text="{Binding User.firstname, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />
        
        
    

    Why the propertychange event is not triggered on updating view?

  • A.T.
    A.T. about 11 years
    i did that but that doesn't work as in view textbox is bind for User.firstname...
  • A.T.
    A.T. about 11 years
    passing empty string or null will notify all properties of the view model has changed...i believe so...
  • Blachshma
    Blachshma about 11 years
    @Arun , that is correct but why would you want all the bindings in your VM to refresh because of a single property?
  • A.T.
    A.T. about 11 years
    actually i am trying to notify the firstname in user model, which is refer in my VM...but can't figure out what's wrong ?
  • A.T.
    A.T. about 11 years
    may be i am wrong but i need to do check it against my model.
  • Blachshma
    Blachshma about 11 years
    As I stated, it's an inner property of the user, so the setter of user is not called...
  • A.T.
    A.T. about 11 years