[英]How can I update a Xamarin Forms ListView's ViewCell properties after the list has been created?
I expect to be able to change a bound property on my custom ViewCell
and it to update the ListView
item - but it appears only to be used to initialise the view and changes are not reflected. 我希望能够更改自定义ViewCell
上的bound属性,并更新ListView
项-但它似乎仅用于初始化视图,并且不会反映更改。 Please tell me what I am missing! 请告诉我我想念的东西!
Here I pick up on the tapped event and attempt to change the string of the ViewCell without success: 在这里,我接听了被窃听的事件,并尝试更改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
}
Here's my ViewCell code: 这是我的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;
}
}
Here is my listview class: 这是我的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; }
}
}
Which i add values to like so: 我添加的值是这样的:
DocChooser.SelectedDocuments.Add(new DocumentChooserList.DocumentType(){
Name = "MyDoc"
});
Using this simple data class: 使用这个简单的数据类:
public class DocumentType
{
public string Name { get; set; }
}
What I'm missing is implementing INotifyPropertyChanged
interface on the data class that is bound to the ViewCell
. 我所缺少的是在绑定到ViewCell
的数据类上实现INotifyPropertyChanged
接口。
In my original implementation the DocumentType class just had simple properties like string Name { get; set; }
在我最初的实现中,DocumentType类仅具有简单的属性,例如string Name { get; set; }
string Name { get; set; }
string Name { get; set; }
, but to have their values reflected in the ViewCell
you need to do implement INotifyPropertyChanged
so that when you change a property it notifies the bound ViewCell
: string Name { get; set; }
,但有自己的价值观体现在ViewCell
你需要做的实现INotifyPropertyChanged
,这样,当你改变它通知绑定的属性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"); } }
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.