简体   繁体   English

创建列表后,如何更新Xamarin Forms ListView的ViewCell属性?

[英]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.

相关问题 如何自定义要在Xamarin Forms ListItem的ViewCell中显示的数据? - How can I customize the data to display in Xamarin Forms ListItem's ViewCell? Xamarin Forms ListView:访问ViewCell的对象数据 - Xamarin Forms ListView: Access to ViewCell's Object data Xamarin.Forms中如何获取ListView的ViewCell数据并发送到另一个页面 - How to get ListView's ViewCell data and send it to another page in Xamarin.Forms 创建HttpClient之后,是否可以更改HttpClientHandler的属性? - Can I change the properties of a HttpClientHandler after the HttpClient has been created? 如何在ListView Xamarin Forms中创建舍入的第一个和最后一个视单元? - How to create a rounded first and last viewcell in ListView Xamarin Forms? 如何通过索引从 xamarin 形式的 ListView 对象获取访问视单元? - How to get access viewcell by index from ListView object in xamarin forms? 如何从 Xamarin Forms 中的自定义 ViewCell 获取 ListView 项目索引? - How to get ListView item index from custom ViewCell in Xamarin Forms? Xamarin Forms - ListView 中的 iOS 动态 ViewCell 大小 - Xamarin Forms - iOS dynamic ViewCell size in a ListView 如何将 View 绑定到 Xamarin 中的 ViewCell - How can I bind View to ViewCell in Xamarin 如何知道在 ListView 中修改了哪个 Xamarin Forms Entry? - How to know which Xamarin Forms Entry has been modified in a ListView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM