【问题标题】:xamarin checkbox inside listview visibility change列表视图可见性更改中的xamarin复选框
【发布时间】:2019-12-19 14:35:31
【问题描述】:

我有一个包含列表视图的 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的item source。

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


    【解决方案1】:

    您需要实现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");
                }
            }
        }
    
       
    }
    

    【讨论】:

    • 我们可以在后端代码中实现它吗?我没有使用 mvvm
    • 只需将上述代码添加到TimeSheetListData即可。
    • 好的兄弟..让我检查一下
    • 你太棒了兄弟..它奏效了。你能给我另一个帮助吗?当我按住视图单元格时,如何为该单个单元格选中复选框?
    • 您可以先接受它,然后创建一个新线程,包含有关该问题的更多详细信息。以便人们可以更好地帮助您。
    猜你喜欢
    • 2014-07-04
    • 1970-01-01
    • 2012-11-26
    • 2019-09-02
    • 2016-02-27
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    相关资源
    最近更新 更多