Home > Software engineering >  How can I tell the NotifyPropertyChanged Method which property changed if the property does not seem
How can I tell the NotifyPropertyChanged Method which property changed if the property does not seem

Time:10-05

Hi I am trying to tell the INotifyPropertyChangedHandler which property changed. Unfortuanatly I think the method only works receiving a string. My code looks like this:

 public class Placeholder
    {
        
        public String ResourceId { get; set; }
        
        public Placeholder(String resourceId)
        {
            
            ResourceId = resourceID;

        }
    }
public partial class MainPage : ContentPage, INotifyPropertyChanged
    {
        public ObservableCollection<Placeholder> List { get; set; }

        public MainPage()
        {
           List = new ObservableCollection<Placeholder>();
           Reset(); //Fills the List with all kinds of Placeholders with implemented Id's
           InitializeComponent();
        }
       
  
        public string  this [int index]
        {
            get
            {
                return List[index].ResourceId;
            }
        } 
// Now I would like to have a method that looks anyhere like this:

private void NotifyPropertyChanged( int index = 0)
        {
            if (PropertyChanged != null)
            {
                // It should somehow tell the System which Property changed by an index
                PropertyChanged(this, new PropertyChangedEventArgs(""));
            }
        }
       
    }


My xaml btw looks something like this:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamarinRocketsPapa"
             x:Class="XamarinRocketsPapa.MainPage"
             x:Name = "Main"
             Padding="5,20,5,5"
             BindingContext="{x:Reference Main}">
 <local:CustomButton
                Source="{Binding [7] , Converter={StaticResource StringToSourceConverter}}"
                Number="7">
 </local:CustomButton>
</ContentPage>

Thank you alot for helping me!

CodePudding user response:

For changes to your indexer property, use the Binding.IndexerName constant.

private void NotifyPropertyChanged( int index = 0)
{
    var handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(Binding.IndexerName));
    }
}

Looking at the source code, this is a constant string with the value "Item[]".

Edit:

For Xamarin, this answer suggests you should just use the string "Item" instead.

private void NotifyPropertyChanged( int index = 0)
{
    var handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs("Item"));
    }
}

CodePudding user response:

This is the solution that works and solves my problem, based on @Richard Deeming's answer and the discussion that followed:

private void NotifyPropertyChanged( int index = 0)         
{             
   var handler = PropertyChanged;           
   if (handler != null)
   {
          handler(this, new PropertyChangedEventArgs($"Item[{index}]"));             
   }
}
  • Related