none
how to raise INotifyPropertyChanged programatically

    Question

  • Hai

    I have a text box which is binded to a property. 

    In Xaml
    
     <TextBox  x:Name="Txt_Name" Text="{Binding M_Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="28"  Width="100"/>
    
    In ViewModel
    
    private string _Name;
            public string M_Name
            {
                get
                {
                    return _Name;
                }
                set
                {
                    _Name= value;
                }
            }
    
    In Code
    Txt_Name.Text = "Hello";

    and I have also implemented INotifyPropertyChanged methods.

    Problem is M_Name is not raising when i assign text in code behind.

    I want to raise this property in code behind.

    Please help me.

    Thanks.


    programmer


    • Edited by babucr Wednesday, December 06, 2017 5:24 PM
    Wednesday, December 06, 2017 5:23 PM

All replies

  • Refer to :

    https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx?f=255&MSPPError=-2147217396&cs-save-lang=1&cs-lang=csharp#code-snippet-2


    Lloyd Sheen

    Wednesday, December 06, 2017 6:04 PM
  • babucr, please refer to sqlguy link. depending on the version of .Net the code below is different but YOU have to call it for properties you want to notify the UI

    set
      {
         _Name = value;
         RaisePropertyChanged("M_Name");
       }

    Also if you are bound, you should almost NEVER set the UI control since it might not be on the same thread. I suggest setting you ViewModel property and it updates the control Text automatically.


    noorbakhsh حميد نوربخش




    • Edited by noorbakhsh Wednesday, December 06, 2017 7:08 PM
    Wednesday, December 06, 2017 7:04 PM
  • Thanks for reply

    I changed to view model like this

    In ViewModel
     
    public int Total
            {
                get
                {
                    Average = (student.English + student.Science + student.Maths) / 3;
                    NotifyPropertyChanged("Average");
                    return (student.English + student.Science + student.Maths);
                }
            }
    
     private int _Average;
            public int Average
            {
    
                get
                {
                   
                    return _Average;
                }
                set
                {
                    _Average = value;
                    NotifyPropertyChanged("Average");
                }
            }
    
    In xaml
      <TextBox x:Name="Txt_Avg"  HorizontalAlignment="Left" Height="27" Margin="519,268,0,0" Text="{Binding Average}" VerticalAlignment="Top" Width="86"/>
    
    And In code
         StudentModel vm = new StudentModel();
         Txt_Avg.DataContext = vm;
    

    I am getting values inside  StudentModel Average property but Text box is not updating.

    What is the problem.

    Thanks.


    programmer

    Thursday, December 07, 2017 5:49 AM
  • You need to notify the UI when the value changes.  Put the notify for Total in the Set for any value that would affect the Total or Average.

    Lloyd Sheen

    Thursday, December 07, 2017 1:49 PM
  • Thanks for reply

    I tried like this but not working.

          Student _student;
            public StudentModel()
            { _student = new Student(); }
            public Student student
            {
                get { return _student; }
                set { _student = value; }
                //set { _student = value; NotifyPropertyChanged("Average"); }
            }
            public int wrappingEnglish
            {
    
                get
                {
                    return student.English;
                }
                set
                {
                    student.English = value; NotifyPropertyChanged("Total");
                    NotifyPropertyChanged("Average");
                }
            }
            public int wrappingMaths
            {
    
                get
                {
                    return student.Maths;
                }
                set
                {
                    student.Maths = value; NotifyPropertyChanged("Total");
                    NotifyPropertyChanged("Average");
                }
            }
            public int Total
            {
                get
                {
                    Average = (student.English + student.Science + student.Maths) / 3;
                    NotifyPropertyChanged("Average");
                    return (student.English + student.Science + student.Maths);
                }
                set
                {
                    Total = value;
                    NotifyPropertyChanged("Average");
                }
            }
    
            private int _Average;
            public int Average
            {
                get
                {
                    return _Average;
                }
                set
                {
                    _Average = value;
                    NotifyPropertyChanged("Average");
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName]string PropertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
                }
            }
        }

    Thanks.


    programmer

    Thursday, December 07, 2017 3:35 PM
  • If the notify is not working then check your immediate window which should show databinding errors.

    Lloyd Sheen

    Thursday, December 07, 2017 3:39 PM
  • Sorry immediate window shows no error

    Please see the project.

    https://www.dropbox.com/s/oszd9093z3agz4g/Student%20TxtBox.zip?dl=0


    programmer


    • Edited by babucr Thursday, December 07, 2017 4:56 PM
    Thursday, December 07, 2017 4:46 PM
  • Dropbox wants me to login so I guess no project.

    Can you place breakpoints in the get for Total and Average?  Check to see if they are called.  I would think you would have a set of properties that hold the items for both Total and Average and when the get is called then calculate the Total or Average.


    Lloyd Sheen

    Thursday, December 07, 2017 5:33 PM
  • Yes total is raising but Average is not hitting break point.

    please see onedrive link

    https://1drv.ms/u/s!AkcLq6QOs-aEhnju7kWddT4Beaw3

    Thanks


    programmer

    Friday, December 08, 2017 1:30 AM
  • I cannot run your app since it uses database files that I do not have.

    Looking at your code I see the following that could be a problem.  You are doing the notify of the Average in the get for Total.  Now I don't know (can't run) what will happen there but you never set Average in the code so even if it retrieves the value it will not be an average.


    Lloyd Sheen

    Friday, December 08, 2017 1:10 PM