如何根据月份向 ObservableCollection 添加项目?
How to add items to an ObservableCollection based on the month?
我有一个 ObservableCollection
用于存储月份事件。我正在添加如下月份的事件:其中 AllItems
是我的 ObservableCollection
.
AllItems = new ObservableCollection<EventsHB>();
EventsHB januvaryGroup = new EventsHB() { month = "January" };
EventsHB februvaryGroup = new EventsHB() { month = "February" };
EventsHB marchGroup = new EventsHB() { month = "March" };
EventsHB aprilGroup = new EventsHB() { month = "April" };
EventsHB mayGroup = new EventsHB() { month = "May" };
EventsHB juneGroup = new EventsHB() { month = "June" };
EventsHB julyGroup = new EventsHB() { month = "July" };
EventsHB augustGroup = new EventsHB() { month = "August" };
EventsHB septemberGroup = new EventsHB() { month = "September" };
EventsHB octoberGroup = new EventsHB() { month = "October" };
EventsHB novemberGroup = new EventsHB() { month = "November" };
EventsHB decemberGroup = new EventsHB() { month = "December" };
foreach (var item in allItem)
{
EventsHBTwo hb = item;
if (hb.month == "January")
{
januvaryGroup.Add(hb.eventTO);
}
else if (hb.month == "February")
{
februvaryGroup.Add(hb.eventTO);
}
else if (hb.month == "March")
{
marchGroup.Add(hb.eventTO);
}
else if (hb.month == "April")
{
aprilGroup.Add(hb.eventTO);
}
else if (hb.month == "May")
{
mayGroup.Add(hb.eventTO);
}
else if (hb.month == "June")
{
juneGroup.Add(hb.eventTO);
}
else if (hb.month == "July")
{
julyGroup.Add(hb.eventTO);
}
else if (hb.month == "June")
{
juneGroup.Add(hb.eventTO);
}
else if (hb.month == "July")
{
julyGroup.Add(hb.eventTO);
}
else if (hb.month == "August")
{
augustGroup.Add(hb.eventTO);
}
else if (hb.month == "September")
{
septemberGroup.Add(hb.eventTO);
}
else if (hb.month == "October")
{
octoberGroup.Add(hb.eventTO);
}
else if (hb.month == "November")
{
novemberGroup.Add(hb.eventTO);
}
else if (hb.month == "December")
{
decemberGroup.Add(hb.eventTO);
}
}
try
{
List<EventsHB> MonthsData = new List<EventsHB>() { null, januvaryGroup, februvaryGroup, marchGroup, aprilGroup, marchGroup, juneGroup, julyGroup, augustGroup, septemberGroup, octoberGroup, novemberGroup, decemberGroup };
int currentMonth = 12;
while (AllItems.Count < 12)
{
AllItems.Add(MonthsData[currentMonth]);
currentMonth++;
if (currentMonth == 13)
currentMonth = 1;
}
}
catch(Exception e)
{
Debug.WriteLine("Event Exception:>>"+e);
}
是否可以根据月份添加事件?如果当前月份是 August
,我需要先将 august
事件添加到集合中。然后 September
到 July
。不使用 12 if 条件可以吗?
您可以使用此逻辑 :- 我已经使用您需要使用模型修改的字符串构建了逻辑 class。
MonthsData 将是您的数据:- januvaryGroup、februvaryGroup 等。您需要在此之前添加一个空组。
List<string> MonthsData = new List<string>() { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
int currentMonth = DateTime.Now.Month;
ObservableCollection<string> list = new ObservableCollection<string>(new List<string>());
while(list.Count < 12)
{
list.Add(MonthsData[currentMonth]);
currentMonth++;
if (currentMonth == 13)
currentMonth = 1;
}
如果您有任何疑问,请告诉我。
我有一个 ObservableCollection
用于存储月份事件。我正在添加如下月份的事件:其中 AllItems
是我的 ObservableCollection
.
AllItems = new ObservableCollection<EventsHB>();
EventsHB januvaryGroup = new EventsHB() { month = "January" };
EventsHB februvaryGroup = new EventsHB() { month = "February" };
EventsHB marchGroup = new EventsHB() { month = "March" };
EventsHB aprilGroup = new EventsHB() { month = "April" };
EventsHB mayGroup = new EventsHB() { month = "May" };
EventsHB juneGroup = new EventsHB() { month = "June" };
EventsHB julyGroup = new EventsHB() { month = "July" };
EventsHB augustGroup = new EventsHB() { month = "August" };
EventsHB septemberGroup = new EventsHB() { month = "September" };
EventsHB octoberGroup = new EventsHB() { month = "October" };
EventsHB novemberGroup = new EventsHB() { month = "November" };
EventsHB decemberGroup = new EventsHB() { month = "December" };
foreach (var item in allItem)
{
EventsHBTwo hb = item;
if (hb.month == "January")
{
januvaryGroup.Add(hb.eventTO);
}
else if (hb.month == "February")
{
februvaryGroup.Add(hb.eventTO);
}
else if (hb.month == "March")
{
marchGroup.Add(hb.eventTO);
}
else if (hb.month == "April")
{
aprilGroup.Add(hb.eventTO);
}
else if (hb.month == "May")
{
mayGroup.Add(hb.eventTO);
}
else if (hb.month == "June")
{
juneGroup.Add(hb.eventTO);
}
else if (hb.month == "July")
{
julyGroup.Add(hb.eventTO);
}
else if (hb.month == "June")
{
juneGroup.Add(hb.eventTO);
}
else if (hb.month == "July")
{
julyGroup.Add(hb.eventTO);
}
else if (hb.month == "August")
{
augustGroup.Add(hb.eventTO);
}
else if (hb.month == "September")
{
septemberGroup.Add(hb.eventTO);
}
else if (hb.month == "October")
{
octoberGroup.Add(hb.eventTO);
}
else if (hb.month == "November")
{
novemberGroup.Add(hb.eventTO);
}
else if (hb.month == "December")
{
decemberGroup.Add(hb.eventTO);
}
}
try
{
List<EventsHB> MonthsData = new List<EventsHB>() { null, januvaryGroup, februvaryGroup, marchGroup, aprilGroup, marchGroup, juneGroup, julyGroup, augustGroup, septemberGroup, octoberGroup, novemberGroup, decemberGroup };
int currentMonth = 12;
while (AllItems.Count < 12)
{
AllItems.Add(MonthsData[currentMonth]);
currentMonth++;
if (currentMonth == 13)
currentMonth = 1;
}
}
catch(Exception e)
{
Debug.WriteLine("Event Exception:>>"+e);
}
是否可以根据月份添加事件?如果当前月份是 August
,我需要先将 august
事件添加到集合中。然后 September
到 July
。不使用 12 if 条件可以吗?
您可以使用此逻辑 :- 我已经使用您需要使用模型修改的字符串构建了逻辑 class。 MonthsData 将是您的数据:- januvaryGroup、februvaryGroup 等。您需要在此之前添加一个空组。
List<string> MonthsData = new List<string>() { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
int currentMonth = DateTime.Now.Month;
ObservableCollection<string> list = new ObservableCollection<string>(new List<string>());
while(list.Count < 12)
{
list.Add(MonthsData[currentMonth]);
currentMonth++;
if (currentMonth == 13)
currentMonth = 1;
}
如果您有任何疑问,请告诉我。