嗨,我想告訴 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 btw 看起來像這樣:
<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>
非常感謝你幫助我!
uj5u.com熱心網友回復:
對于更改您的索引屬性使用的Binding.IndexerName常量。
private void NotifyPropertyChanged( int index = 0)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(Binding.IndexerName));
}
}
查看源代碼,這是一個帶有值的常量字串"Item[]"。
編輯:
對于 Xamarin,此答案建議您只使用字串"Item"。
private void NotifyPropertyChanged( int index = 0)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs("Item"));
}
}
uj5u.com熱心網友回復:
這是基于@Richard Deeming 的回答和隨后的討論的有效解決方案并解決了我的問題:
private void NotifyPropertyChanged( int index = 0)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs($"Item[{index}]"));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313957.html
