noe*_*cus 2 c# data-binding xamarin.forms
我希望能够更改自定义的绑定属性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
}
Run Code Online (Sandbox Code Playgroud)
这是我的 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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的列表视图类:
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
我添加了这样的值:
DocChooser.SelectedDocuments.Add(new DocumentChooserList.DocumentType(){
Name = "MyDoc"
});
Run Code Online (Sandbox Code Playgroud)
使用这个简单的数据类:
public class DocumentType
{
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我缺少的是INotifyPropertyChanged在绑定到ViewCell.
在我最初的实现中,DocumentType 类只有简单的属性,例如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"); } }
...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3724 次 |
| 最近记录: |