xamarin.forms - 列表视图可见性更改中的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。但它只会在列表视图滚动时可见。我该如何解决这个问题?
解决方案
您需要实现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");
}
}
}
}
推荐阅读
- swiftui - 如何在 swiftui 中弹出到 TabView 应用程序中的特定视图。我也使用了 StackNavigation 但不在 swiftui 中工作
- python - 如何避免在列表中附加坏字符(Python)?
- dynamodb-queries - 如何在 Dynamodb 聊天应用程序中通过文本查找消息?
- linux - Docker 容器挂载文件未在虚拟机中更新
- python - 如何删除 BoxLayout?
- python - 如何从 Python 中的 pandas 数据框中获取 networkx 图的分支作为列表?
- python - 如果我可以使用 post 为什么要使用 put 方法
- docker - Docker Private Registry 仅适用于 HTTPS 或 HTTP 不能同时使用两者
- css - 如何使 Web 小部件样式与不同主题兼容?
- html - 如何在 HTML-CSS 中为单边添加圆角