【问题标题】:Xamarin, ListView, How to get values from checkbox in ListView?Xamarin,ListView,如何从 ListView 中的复选框中获取值?
【发布时间】:2020-04-14 09:08:14
【问题描述】:

我有一个带有 LabelCheckbox

ListView

我想实现一个按钮列表器,它将从我的 ListView

中获取所有选中的项目

这是 ListView

<ListView ItemsSource="{Binding OCParticipantsTable}" 
HasUnevenRows="True" 
x:Name="dsfdf">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
 <StackLayout>
 <Label Text="{Binding FirstName_}"/>
 <CheckBox/>
 </StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

这是 ItemsSource 属性:

private ObservableCollection<ParticipantsTable> _OCParticipantsTable = 
            new ObservableCollection<ParticipantsTable>();
        public ObservableCollection<ParticipantsTable> OCParticipantsTable
        {
            get { return _OCParticipantsTable; }
            set
            {
                if (_OCParticipantsTable != value)
                {
                    _OCParticipantsTable = value;
                    OnPropertyChanged("ListOfItems");
                }
            }
        }

如何实现一个按钮列表器,它将从我的 ListView 中获取所有选中的项目?

类似的东西:

foreach (var pt in dsfdf.ItemsSource)
            {
                if (pt.CheckBox.IsChecked)
                {
                    // do something...
                }
            }

【问题讨论】:

  • 使用绑定。将复选框绑定到 VM 中的属性,然后只需检查该属性即可获取所选项目。

标签: listview checkbox xamarin.forms


【解决方案1】:

正如 Jason 所说,您将复选框的 IsChecked 属性绑定到一个属性,然后您可以 foreach OCParticipantsTable 以获取复选框的 IsChecked 属性为 true 的所有项目。

 <ListView
            x:Name="dsfdf"
            HasUnevenRows="True"
            ItemsSource="{Binding OCParticipantsTable}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding name}" />
                            <CheckBox IsChecked="{Binding ischecked}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <Button
            x:Name="btn1"
            Clicked="Btn1_Clicked"
            Text="getdata" />

这是模型,有一些属性,请包含一个bool属性绑定CheckBox,并实现INotifyPropertychanged接口,通知数据发生变化。

 public class ParticipantsTable:ViewModelBase
{
    public string name { get; set; }
    private bool _ischecked;
    public bool ischecked
    {
        get { return _ischecked; }
        set
        {
            _ischecked = value;
            RaisePropertyChanged("ischecked");
        }
    }
}

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));
        }
    }
}
 public partial class Page15 : ContentPage
{
   public ObservableCollection<ParticipantsTable> OCParticipantsTable { get; set; }
    public Page15()
    {
        InitializeComponent();

        OCParticipantsTable = new ObservableCollection<ParticipantsTable>()
        {
            new ParticipantsTable(){name="cherry",ischecked=false },
             new ParticipantsTable(){name="barry",ischecked=true },
              new ParticipantsTable(){name="annine",ischecked=false },
               new ParticipantsTable(){name="wendy",ischecked=false },
               new ParticipantsTable(){name="leo",ischecked=true },
               new ParticipantsTable(){name="alex",ischecked=false }
        };
        this.BindingContext = this;
    }

    private void Btn1_Clicked(object sender, EventArgs e)
    {
        foreach(var pt in OCParticipantsTable)
        {
            if(pt.ischecked)
            {
                //do something you want to do
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    相关资源
    最近更新 更多