2
我希望能夠更改我的自定義ViewCell
上的綁定屬性,並且它將更新ListView
項目 - 但它似乎只用於初始化視圖並且不會反映更改。請告訴我我錯過了什麼!如何在創建列表後更新Xamarin表單ListView的ViewCell屬性?
在這裏,我拿起了竊聽事件並試圖改變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; }
}
}
其中我增加了像這樣的值:
DocChooser.SelectedDocuments.Add(new DocumentChooserList.DocumentType(){
Name = "MyDoc"
});
使用這個簡單的數據類:
public class DocumentType
{
public string Name { get; set; }
}