【问题标题】:How can I tell the NotifyPropertyChanged Method which property changed if the property does not seem to have a name?如果属性似乎没有名称,我如何告诉 NotifyPropertyChanged 方法哪个属性发生了变化?
【发布时间】:2021-11-24 22:00:57
【问题描述】:

您好,我想告诉 INotifyPropertyChangedHandler 哪个属性发生了变化。不幸的是,我认为该方法仅适用于接收字符串。我的代码如下所示:

 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(""));
            }
        }
       
    }


顺便说一句,我的 xaml 看起来像这样:

<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>

非常感谢您帮助我!

【问题讨论】:

  • 请问为什么不绑定到您的List 属性并使用其预定义的索引器运算符?

标签: c# xaml xamarin.forms data-binding inotifypropertychanged


【解决方案1】:

要更改您的索引器属性,请使用the Binding.IndexerName constant

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

查看the source code,这是一个值为"Item[]"的常量字符串。

编辑:

对于 Xamarin,this answer 建议您只使用字符串 "Item"

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

【讨论】:

  • 非常感谢!这个常量究竟应该放在哪里,它是如何工作的?
  • 已经定义好了;它是 WCF PresentationFramework 库的一部分。
  • 这就是我的想法...如果我复制粘贴您的代码 Binding.IndexerName 下划线红色...我错过了什么@RichardDeeming 吗?
  • 您的文件顶部是否有 using System.Windows.Data; 行,以及对 PresentationFramework 程序集的引用?
  • 好吧,我会写handler(this, new PropertyChangedEventArgs("Item")); ? @Richard Deeming ?
【解决方案2】:

根据@Richard Deeming 的回答和随后的讨论,这是有效并解决了我的问题的解决方案:

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

【讨论】:

    猜你喜欢
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 2020-06-06
    相关资源
    最近更新 更多