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

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,399 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,678 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,286 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 40,271 Reputation points Microsoft Vendor
    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,116 Reputation points
    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