【问题标题】:How to change selected item property in ListView如何更改 ListView 中的选定项属性
【发布时间】:2020-05-06 19:28:32
【问题描述】:

我需要更改 ListView(Xamarin Forms) 中的选定项属性。

List<Item> _source = new List<Item>();
_source.Add(new Item { Title = "First item" });

ListView.ItemsSource = _source;

我正在尝试更改我在列表中选择的项目属性。

Item item = ListView.SelectedItem as Item;
item.Title = "Changed title";

告诉我我做错了什么? 我也在尝试选择一个带有索引的项目。由于缺少 SelectedIndex 属性,我尝试这样做:

int selector = 1;
ListView.SelectedItem = _source[selector];

但它也没有成功。

请帮帮我。 谢谢。

【问题讨论】:

  • 你的Item类需要实现INotifyPropertyChanged
  • 我是 Xamarin Forms 的新手,我不知道 INotifyPropertyChanged。
  • INPC 是一个 c# 数据绑定概念,它与 XF 无关。网上有数千篇文章将向您展示如何实施它
  • 谢谢。我试图找到那个。
  • @Jason 让模型类实现INotifyPropertyChanged 好吗?

标签: c# listview xamarin.forms collectionview


【解决方案1】:

正如 Jason 所说,如果要先更改 Item 属性,则需要为 Item 类实现 INotifyPropertyChanged 接口。

 public class Item:ViewModelBase
{
    private string _Title;
    public string Title
    {
        get { return _Title; }
        set
        {
            _Title = value;
            RaisePropertyChanged("Title");
        }
    }
}

ViewModelBase 是实现 INotifyPropertyChanged 接口的类。

public class ViewModelBase : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

那我建议你可以用ObservableCollection代替List,因为ObservableCollection代表了一个动态的数据集合,当item被添加、删除或者整个list刷新时提供通知。

最后,使用 ListView 绑定并更改 selecteditem 属性。

<StackLayout>
        <ListView x:Name="listview1" ItemsSource="{Binding source}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding Title}" />
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <Button
            x:Name="btn1"
            Clicked="btn1_Clicked"
            Text="update data" />
    </StackLayout>

public partial class Page18 : ContentPage
{
    public ObservableCollection<Item> source { get; set; }
    public Page18()
    {
        InitializeComponent();

        source = new ObservableCollection<Item>()
        {
            new Item(){Title="title 1"},
            new Item(){Title="title 2"},
            new Item(){Title="title 3"},
            new Item(){Title="title 4"},
            new Item(){Title="title 5"}
        };
        this.BindingContext = this;
    }

    private void btn1_Clicked(object sender, EventArgs e)
    {
        Item item = listview1.SelectedItem as Item;
        item.Title = "Changed title";
    }
}

关于Binding和INotifyPropertyChanged,请看:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm

【讨论】:

  • 感谢您的帮助?☺️?
  • @WinSoft 如果我的回复解决了你的问题,请记得将我的回复标记为答案,有利于其他人找到这个答案,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多