【发布时间】: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