在列表视图中单击修改标签文本
Modify label text on click in listview
这是我的代码:
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));
}
}
}
如何在点击图片时更改列表视图中与名称绑定的标签文本?目前,点击没有改变,列表视图的目标项目中没有显示“新文本”。
Note: XAML here
Note: Debugging in Android Device
您必须在 setter 中为每个 属性 执行 OnPropertyChanged() 方法,以便通知绑定和 'refresh' 文本。
private string name;
public string Name
{
get
{
return name;
}
set
{
if (name == value)
{
return;
}
name = value;
OnPropertyChanged();
}
}
不要忘记更新绑定路径以便使用 属性 而不是私有字段...({绑定名称})
这是我的代码:
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));
}
}
}
如何在点击图片时更改列表视图中与名称绑定的标签文本?目前,点击没有改变,列表视图的目标项目中没有显示“新文本”。
Note: XAML here
Note: Debugging in Android Device
您必须在 setter 中为每个 属性 执行 OnPropertyChanged() 方法,以便通知绑定和 'refresh' 文本。
private string name;
public string Name
{
get
{
return name;
}
set
{
if (name == value)
{
return;
}
name = value;
OnPropertyChanged();
}
}
不要忘记更新绑定路径以便使用 属性 而不是私有字段...({绑定名称})