简体   繁体   中英

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. 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:

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:

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:

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 .

In my original implementation the DocumentType class just had simple properties like 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 :

    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"); } } 

        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM