who added the method in to "PropertyChanged"? Questions about using "INotifyPropertyChanged"

兰树豪 381 Reputation points
2022-08-11T03:19:24.13+00:00

please see the picture, who added the method in to "PropertyChanged", I didn't do anything , but "PropertyChangedcan" be called , and "PropertyChanged" is not null value
230259-inotifypropertychanged.png

Developer technologies | Windows Presentation Foundation
Developer technologies | .NET | Other
Developer technologies | C#
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2022-08-11T05:55:39.163+00:00

    Hi,@43061275. I have not been able to reproduce your problem. Could you show minimal code that reproduces the problem?
    xaml:

     <Window.DataContext>  
        <local:Person/>  
    </Window.DataContext>  
    <StackPanel>  
        <TextBox x:Name="tb" Text="{Binding PersonName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="50" Background="AliceBlue"/>  
        <TextBlock x:Name="tb1" Text="{Binding PersonName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="50" Background="AliceBlue"/>  
       
    </StackPanel>  
    

    codebehind:

     public class Person: INotifyPropertyChanged  
      {  
        private string name = "hello";  
        public string PersonName  
        {  
          get { return name; }  
          set  
          {  
            name = value;  
            ChangeProperty();  
          }  
        }  
        public event PropertyChangedEventHandler PropertyChanged;  
        protected void ChangeProperty([CallerMemberName] string name = null)  
        {  
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));  
        }  
      }  
    

    The result:

    230269-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    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.


1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-08-11T08:26:24.693+00:00

    When you implement INotifyPropertyChanged Visual Studio adds PropertyChangedEventHandler and OnPropertyChanged.

    To see how it's done, add a new class to your project, implement INotifyPropertyChanged and Visual Studio will indicated missing members and will offer to create them for you.

    Note I'm curious why your code does not appear as follows which uses CallerMemberName.

    public class VendorModel : INotifyPropertyChanged  
    {  
    	private readonly CollectionView _vendorEntries;  
    	private string _vendorEntry;  
      
    	public VendorModel()  
    	{  
    		IReadOnlyList<Vendor> list = References.Vendors();  
    		_vendorEntries = new CollectionView(list);  
    	}  
      
    	public CollectionView VendorEntries => _vendorEntries;  
      
    	public string VendorEntry  
    	{  
    		get => _vendorEntry;  
    		set  
    		{  
      
    			if (_vendorEntry == value)  
    			{  
    				return;  
    			}  
      
    			_vendorEntry = value;  
      
    			OnPropertyChanged();  
    		}  
    	}  
      
    	public event PropertyChangedEventHandler PropertyChanged;  
    	protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)  
    	{  
    		PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
    	}  
    }  
    
    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.