如何刷新下拉菜单并不总是刷新数据

本文关键字:刷新 数据 何刷新 下拉菜单 | 更新日期: 2023-09-27 18:14:55

在这段代码中,我们有一个下拉组合框,供用户选择按哪个状态显示数据。在某些情况下,它不刷新-我如何在这里添加一个简单的刷新?从这里开始:

       using System;
       using System.Collections.Generic;
        using System.Linq;
       using System.Text;
       using OPTFDashboard.Common.Modules.Schedules.DataModel;
       using OPTFDashboard.DataAccess;
       using OPTFDashboard.DataModel;
   namespace OPTFDashboard.Common.Modules.Schedules.DataAccess
{
   public class SchedulesRepository
 {
    public static void Summary(IEnumerable<Facility> facilities, DateTime fromDate, DateTime toDate, Action<MonthlySchedules> completionHandler)
    {
        DBHelper.Execute(
        DBHelper.StoredProcedure("OGEN.DBD_GET_MONTHLY_SCHEDULES",
        new DBHelper.Parameter("@FACILITYKEY", String.Join(",", facilities.Select(f =>  String.Format("{0}", f.FacilityKey)))),
        new DBHelper.Parameter("@FromDate", fromDate),
        new DBHelper.Parameter("@ToDate", toDate)
        ),
        (reader) =>
        {
            completionHandler((reader == null || !reader.Read()) ? null :
            new MonthlySchedules((Decimal)reader["COMPLETED"], (Decimal)reader["UNCOMPLETED"], (Decimal)reader["LATE"]));
        });
    }
    public static void Mixed(IEnumerable<Facility> facilities, Action<List<ScheduleMIXED>> completionHandler)
    {
        DBHelper.Execute(
        DBHelper.StoredProcedure("OGEN.DBD_GET_SCHEDULE_MIX",
            new DBHelper.Parameter("@FACILITYKEY", String.Join(",", facilities.Select(f => String.Format("{0}", f.FacilityKey))))
            //,
            //new DBHelper.Parameter("@UNITSTR", unit),
            //new DBHelper.Parameter("@FromDate", fromDate),
            //new DBHelper.Parameter("@ToDate", toDate)
        ),
        (reader) =>
        {
            var schedules = new List<ScheduleMIXED>();
            while (reader != null && reader.Read())
                schedules.Add(new ScheduleMIXED((String)reader["ROW_NAME"], (Decimal)reader["COMP"], (Decimal)reader["EOT_COT"], (Decimal)reader["PPS"], (Decimal)reader["QUART"], (Decimal)reader["TRACK"], (Decimal)reader["OTHER"]));
            completionHandler(schedules);
        });
    }


    public static void Details(IEnumerable<Facility> facilities, String unit, String type, DateTime fromDate, DateTime toDate, Action<List<Schedule>> completionHandler)
    {
        String storeName = String.Empty;
        switch (type)
        //  switch (type.Trim().ToUpper())
        {

            case "NOT STARTED - LATE":
                storeName = "OGEN.DBD_GET_SCHEDULE_LATE_DETAIL";
                break;

            case "COMPLETED":
                storeName = "OGEN.DBD_GET_SCHEDULE_COMPLETED_DETAIL";
                break;
            case "INCOMPLETE":
                storeName = "OGEN.DBD_GET_SCHEDULE_UNCOMPLETED_DETAIL";
                break;

            case "All":
                storeName = "OGEN.DBD_GET_SCHEDULE_ALL_DETAIL";
                break;
            case "SUBMITTED":
                storeName = "OGEN.DBD_GET_SCHEDULE_SUBMITTED_DETAIL";
                break;
            // new ones:
            case "SUBMITTED LATE":
                storeName = "OGEN.DBD_GET_SCHEDULE_SUBMITTED_LATE";
                break;
            case "NOT STARTED":
                storeName = "OGEN.DBD_GET_SCHEDULE_NOT_STARTED";
                break;
            case "COMPLETED LATE":
                storeName = "OGEN.DBD_GET_SCHEDULE_COMPLETED_LATE";
                break;
            case "INCOMPLETE LATE":
                storeName = "OGEN.DBD_GET_SCHEDULE_UNCOMPLETED_LATE";
                break;
        }

        DBHelper.Execute(
        DBHelper.StoredProcedure(storeName,
        new DBHelper.Parameter("@FACILITYKEY", String.Join(",", facilities.Select(f => String.Format("{0}", f.FacilityKey)))),
        new DBHelper.Parameter("@UNITSTR", unit),
        new DBHelper.Parameter("@FromDate", fromDate),
        new DBHelper.Parameter("@ToDate", toDate)
        ),
        (reader) =>
        {
            var schedules = new List<Schedule>();
            if (reader != null && !reader.IsClosed)
                while (reader.Read())
                {
                    Schedule objSchedule = new Schedule() { FACILITY_KEY = (String)reader ["FACILITY_KEY"], UNIT = (String)reader["UNIT_CODE"], PATIENT_ID = Convert.ToString(reader["PATIENT_ID"]).Trim(), PATIENT_NAME = (!DBNull.Value.Equals(reader["PATIENT_ID"]) ? (String)reader["PATIENT_NAME"] : String.Empty) };
                    // reference date taken out.
                    //   if (!DBNull.Value.Equals(reader["REFERENCE_DATE"]))
                    {
                        //      objSchedule.REFERENCE_DATE = (DateTime)reader["REFERENCE_DATE"]; 
                    }
                    if (!DBNull.Value.Equals(reader["A3A_DATE_USER"]))
                    {
                        objSchedule.A3A_DATE_USER = (DateTime)reader["A3A_DATE_USER"];
                    }
                    objSchedule.ASSESSMENTS = (String)reader["ASSESSMENTS"];
                    if (!DBNull.Value.Equals(reader["BASE_REASON"]))
                    {
                        objSchedule.BASE_REASON = (String)reader["BASE_REASON"];
                    }
                    if (!DBNull.Value.Equals(reader["TRACK_DESC"]))
                    {
                        objSchedule.TRACK_DESC = (String)reader["TRACK_DESC"];
                    }

                    schedules.Add(objSchedule);
                }
            completionHandler(schedules);
        });
    }

}

}

下面是加载combobox 的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using OPTFDashboard.ViewModel;
using System.Collections.ObjectModel;
using OPTFDashboard.Common.Ribbon;
using OPTFDashboard.Common.Utility;
using OPTFDashboard.DataModel;
using System.Windows.Data;
using OPTFDashboard.Common.Modules.Schedules.DataAccess;
namespace OPTFDashboard.Common.Modules.Schedules.ViewModels
{
    [Export]
    class ScheduleViewModel : TabViewModel, ISelectedContentTab
    {
        //
        private readonly String[] _assessmentType = new String[] { "All", "INCOMPLETE", "COMPLETED", "COMPLETED LATE", "INCOMPLETE LATE", "NOT STARTED - LATE", "NOT STARTED", "SUBMITTED", "SUBMITTED LATE" };
        //pk display ALL as default in detail tab.
        //
        public ScheduleViewModel()
            : base()
        {
            DisplayName = "Schedules";
            StartDate = DateTime.Now.AddMonths(-6);
            EndDate = DateTime.Now;
            GroupDataCollection = new ObservableCollection<GroupData>()
{
    // here is the combo box
       RibbonControlHelper.CreateFacilitySelection()
        , new GroupData("Criterria"
        , RibbonControlHelper.CreateDateSelection(StartDate,EndDate,(s, e) => { StartDate = s;    EndDate = e; RefreshData(); })
        , RibbonControlHelper.CreateUnitSelection(UnitChanged)
        , RibbonControlHelper.CreateComboBox("Assessment", "Assessment", "Select Assessment to     show.", _assessmentType, (type) => { AssessmentType = type; })
)
};

        }
        protected override void RefreshData()
        {
            if (FacilitiesAreChanging) { return; }
            Loading = true;
            SchedulesRepository.Details(FacilitySelectionService.SelectedFacilities, UnitCode, AssessmentType, StartDate, EndDate,
            (schedules) =>
            {
                var data = new ListCollectionView(schedules);
                data.GroupDescriptions.Add(new PropertyGroupDescription("FACILITY_KEY"));
                data.GroupDescriptions.Add(new PropertyGroupDescription("UNIT"));
                Data = data;
                Loading = false;
            });
        }

        private ListCollectionView _Data;
        public ListCollectionView Data
        {
            get { return _Data; }
            set { this.SetReferenceProperty("Data", ref _Data, value); }
        }
        private DateTime startDate;
        public DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }
        private DateTime endDate;
        public DateTime EndDate
        {
            get { return endDate; }
            set { endDate = value; }
        }

        public ObservableCollection<GroupData> GroupDataCollection { get; private set; }

        private String UnitCode { get; set; }
        private void UnitChanged(Unit unit)
        {
            UnitCode = unit == null ? "" : unit.Description;
            RefreshData();
        }
        private String _Type;
        private String AssessmentType
        {
            get { return _Type; }
            set { if (this.SetReferenceProperty("AssessmentType", ref _Type, value)) { RefreshData(); } }
        }
    }
}

如何刷新下拉菜单并不总是刷新数据

将UserControl的DataContext设置为实现INotifyPropertyChanged并具有ComboBoxSource属性的东西(ViewModel)。然后将ComboBox ItemsSource绑定到属性名。当你想要刷新时,只需在ViewModel上设置属性。PropertyChanged将触发,ItemsSource将自动更新。

将您的ComboBox绑定到ObservableCollection,然后无论何时您从集合中添加或删除项目,您的UI将自动更新

<ComboBox ItemsSource="{Binding SomeCollection}" />

其中SomeCollection定义为

public ObservableCollection<string> SomeCollection { get; set; }

任何时候你添加或删除一个项目到SomeCollection, UI将得到自动更新,因为你使用的ObservableCollection