这是我的代码:
public partial class MyGS: ContentPage {
private GestioneSchedeViewModel _viewModel;
public MyGS() {
InitializeComponent();
BindingContext = _viewModel = new MyGSViewModel();
}
private void modifySch(object sender, EventArgs e)
{
SchedeItem item = (SchedeItem)((Image)sender).BindingContext;
if (item == null) { return; }
_viewModel.modifyItem(item.realm_id, item.list_id);
}
}
public class MyGSViewModel: INotifyCollectionChanged {
public event NotifyCollectionChangedEventHandler CollectionChanged;
public ObservableCollection < SchItem > Items {get;private set;}
public MyGSViewModel() {
Items = new ObservableCollection<SchItem>();
//Item Population
}
public void modificaItem(int rid, int lid)
{
SchedeItem myItem = Items[lid];
myItem.name = "New Text";
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class SchItem : INotifyPropertyChanged {
public int realm_id {get;set;}
public int list_id {get;set;}
public int name {get;set;}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
如何在图片点击时更改listview中带有名称的标签文字?目前,点击不会改变,并且列表视图中的目标项目中不显示“新文本”。
注意:XAML here
注意:在Android设备中进行调试
答案 0 :(得分:2)
您必须在setter中为每个属性执行OnPropertyChanged()方法,以便通知绑定并“刷新”文本。
private string name;
public string Name
{
get
{
return name;
}
set
{
if (name == value)
{
return;
}
name = value;
OnPropertyChanged();
}
}
不要忘记更新Binding路径以使用属性而不是私有字段...({Binding Name})