过滤级联 CheckedListBox 并保留项目的检查状态

Filter cascading CheckedListBox and preserve check state of the items

前几天问过,答案完美解决了我的问题。我还有另一个与我自己相关的问题 post。

当我通过检查第一个(专业)Checkedlistbox 项更改数据源时,第二个(医生)Checkedlistbox 项的 CheckState 将被保存。这是我的代码:

CheckedListBoxItem class:

/// <summary>
/// A class to svae status of checkboxes when datasource changes.
/// </summary>
/// <typeparam name="T"></typeparam>
public class CheckedListBoxItem<T>
{
    public CheckedListBoxItem(T item)
    {
        DataBoundItem = item;
    }
    public T DataBoundItem { get; set; }
    public CheckState CheckState { get; set; }
    public override string ToString() { return DataBoundItem.ToString(); }
}

SpecialityCheckListDoctorCheckList 与第一个和第二个 Checkedlistbox 控件相关:

public class SpecialityCheckList
{
    public int SpecialtyID { get; set; }
    public string Title { get; set; }
    public override string ToString() { return Title; }
}

public class DoctorCheckList
{
    public int DoctorID { get; set; }
    public string Name { get; set; }
    public int? SpecialityId { get; set; }
    public override string ToString() { return Name; }
}

表单代码:

BindingList<CheckedListBoxItem<DoctorCheckList>> doctors = new BindingList<CheckedListBoxItem<DoctorCheckList>>();

private void ReportVisitSocialInsurance_Load(object sender, EventArgs e)
{
     SpecialtyTypeCheckbox.DataSource = new BindingList<SpecialityCheckList>(_Specialty.SelectTbl_SpecialityCheckListBox());

     DoctorsIDCheckedlistbox.DataSource = doctors;
     doctors.ListChanged += Doctors_ListChanged;
}

private void Doctors_ListChanged(object sender, ListChangedEventArgs e)
{
     for (var i = 0; i < DoctorsIDCheckedlistbox.Items.Count; i++)
     {
         DoctorsIDCheckedlistbox.SetItemCheckState(i, ((CheckedListBoxItem<DoctorCheckList>)DoctorsIDCheckedlistbox.Items[i]).CheckState);
     }
}

private void SpecialtyTypeID_ItemCheck(object sender, ItemCheckEventArgs e)
{
     var item = (SpecialityCheckList)SpecialtyTypeCheckbox.Items[e.Index];
     if (e.NewValue == CheckState.Checked)
     {
          _Doctors.SelectTbl_DoctorsCheckListBox(item.SpecialtyID)
                    .Select(s => new CheckedListBoxItem<DoctorCheckList>(s)).ToList()
                    .ForEach(f => doctors.Add(f));
     } else {
          doctors
                    .Where(w => w.DataBoundItem.SpecialityId == item.SpecialtyID)
                    .ToList()
                    .ForEach(f => doctors.Remove(f));
     }
}

private void DoctorsID_ItemCheck(object sender, ItemCheckEventArgs e)
{
      ((CheckedListBoxItem<DoctorCheckList>)DoctorsIDCheckedlistbox.Items[e.Index]).CheckState = e.NewValue;
}

如果我搜索医生的 Checkedlistbox 项目并选中一个项目,当我清除搜索文本框以查看所有医生姓名时,选中的项目与我在搜索时选中的项目不同。我认为这是因为它使用了索引。这是我搜索时的代码:

private void DoctorsNameTextbox_TextChanged(object sender, EventArgs e)
{
     BindingList<CheckedListBoxItem<DoctorCheckList>> doctorsSerach =
                ObjectCloning.CloneJson<BindingList<CheckedListBoxItem<DoctorCheckList>>>(doctors);

     doctorsSerach
                .Where(w => !w.DataBoundItem.Name.Contains(DoctorsNameTextbox.Text))
                .ToList()
                .ForEach(f => doctorsSerach.Remove(f));

     DoctorsIDCheckedlistbox.DataSource = doctorsSerach;
}

问题是,例如,如果我搜索名称 Ali,它会显示 3 个项目。当我选中第 2 个项目并清除搜索文本框时,已选中索引 1(从零开始)的项目。

在处理searchTextBoxTexctChanged事件时,可以检查文本是否为空,将数据源设置为医生列表,否则将数据源设置为过滤后的医生列表.在这两种情况下,设置数据源后,将复选标记与数据源同步:

private void searchTextBox_TextChanged(object sender, EventArgs e)
{
    if(string.IsNullOrEmpty(searchTextBox.Text))
    {
        doctorsCheckedListBox.DataSource = doctors;
    }
    else
    {
        var filteredDoctors = 
            new BindingList<CheckedListBoxItem<Doctor>>
            (doctors.Where(x => x.DataBoundItem.Name.StartsWith(searchTextBox.Text))
            .ToList());
        doctorsCheckedListBox.DataSource = filteredDoctors;
    }
    for (var i = 0; i < doctorsCheckedListBox.Items.Count; i++)
    {
        doctorsCheckedListBox.SetItemCheckState(i,
            ((CheckedListBoxItem<Doctor>)doctorsCheckedListBox.Items[i]).CheckState);
    }
}