C# 如何更新Xamarin表单列表视图';创建列表后的ViewCell属性?
我希望能够更改自定义C# 如何更新Xamarin表单列表视图';创建列表后的ViewCell属性?,c#,data-binding,xamarin.forms,C#,Data Binding,Xamarin.forms,我希望能够更改自定义ViewCell上的绑定属性,并将其用于更新ListView项,但它似乎仅用于初始化视图,并且不会反映更改。请告诉我我错过了什么 在这里,我选择点击事件并尝试更改ViewCell的字符串,但没有成功: private void DocChooser_ItemTapped(object sender, ItemTappedEventArgs e) { var tappedItem = e.Item as DocumentChooserList.DocumentType;
ViewCell
上的绑定属性,并将其用于更新ListView
项,但它似乎仅用于初始化视图,并且不会反映更改。请告诉我我错过了什么
在这里,我选择点击事件并尝试更改ViewCell的字符串,但没有成功:
private void DocChooser_ItemTapped(object sender, ItemTappedEventArgs e)
{
var tappedItem = e.Item as DocumentChooserList.DocumentType;
tappedItem.Name = "Tapped"; // How can I change what a cell displays here? - this doesn't work
}
以下是我的ViewCell代码:
class DocumentCellView : ViewCell
{
public DocumentCellView()
{
var OuterStack = new StackLayout()
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
Label MainLabel;
OuterStack.Children.Add(MainLabel = new Label() { FontSize = 18 });
MainLabel.SetBinding(Label.TextProperty, "Name");
this.View = OuterStack;
}
}
这是我的listview类:
public class DocumentChooserList : ListView
{
public List<DocumentType> SelectedDocuments { get; set; }
public DocumentChooserList()
{
SelectedDocuments = new List<DocumentType>();
this.ItemsSource = SelectedDocuments;
this.ItemTemplate = new DataTemplate(typeof(DocumentCellView));
}
// My data-binding class used to populate ListView and hopefully change as we go
public class DocumentType
{
public string Name { get; set; }
}
}
使用此简单数据类:
public class DocumentType
{
public string Name { get; set; }
}
我缺少的是在绑定到
ViewCell
的数据类上实现INotifyPropertyChanged
接口
在我最初的实现中,DocumentType类只有一些简单的属性,如string Name{get;set;}
,但要在ViewCell
中反映它们的值,需要实现inotifPropertyChanged
,以便在更改属性时通知绑定的ViewCell
:
public class DocumentType : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string nameOfProperty)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(nameOfProperty));
}
private string _Name;
public string Name { get { return _Name; } set { _Name = value; OnPropertyChanged("Name"); } }
...
}
}
public class DocumentType : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string nameOfProperty)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(nameOfProperty));
}
private string _Name;
public string Name { get { return _Name; } set { _Name = value; OnPropertyChanged("Name"); } }
...
}
}