首页 > 解决方案 > 列表视图可见性更改中的xamarin复选框

问题描述

我有一个包含列表视图的 xamarin.forms 应用程序。我为列表视图的视图单元实现了长按手势。哪个工作正常。我想做的是,在我的列表视图中有一个复选框,它的可见性属性设置为绑定到数据模型。默认情况下它将是错误的。如果我长按视图单元格,我希望所有复选框都可见。我的目的是多选列表视图。我怎样才能做到这一点?

我的数据模型

 public class TimeSheetListData
        {       
            public string StartDate { get; set; }
            public string EndDate { get; set; }
            public bool Selected { get; set; }
            public bool IsCheckBoxVisible { get; set; }
        }

我只是将 API 数据设置为 listview 的项目源。

ObservableCollection<TimeSheetListData> resultObjForApprovedTimeSheetList = new ObservableCollection<TimeSheetListData>();

API调用后,

 TimesheetListView.ItemsSource = resultObjForApprovedTimeSheetList;

我的 Longpress 事件和更改复选框的可见性。

   private void CustomView_LongPressEvent(object sender, EventArgs e)
            {                                
            foreach (TimeSheetListData TS in resultObjForApprovedTimeSheetList)
            {

                TSData.IsCheckBoxVisible = true;
            }
                TimesheetListView.ItemsSource = null;
                TimesheetListView.ItemsSource = resultObjForApprovedTimeSheetList
            }

它将复选框的可见性更改为true。但它只会在列表视图滚动时可见。我该如何解决这个问题?

标签: xamarin.forms

解决方案


您需要实现INotifyPropertyChanged接口,以便我们可以在运行时更新 UI。

模型

public class TimeSheetListData: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public string StartDate { get; set; }
    public string EndDate { get; set; }

    private bool selected;
    public bool Selected {

        get 
        {
            return selected;
        }

        set
        {
            if (value!= null)
            {
                selected = value;
                NotifyPropertyChanged("Selected");
            }
        }
    }

    private bool isCheckBoxVisible;
    public bool IsCheckBoxVisible
    {

        get
        {
            return isCheckBoxVisible;
        }

        set
        {
            if (value != null)
            {
                isCheckBoxVisible = value;
                NotifyPropertyChanged("IsCheckBoxVisible");
            }
        }
    }

   
}

推荐阅读