交替突出显示wpf日历

时间:2018-02-22 19:35:56

标签: c# wpf datepicker calendar

在DatePicker工具中选择日期或在TextBox中键入一个日期时, 如何在选定日期之后每隔三个日期突出显示wpf日历单元格? (3个日期突出显示,3个日期不突出显示,并且继续......)。

我使用了这段代码,但是间隔在MontlyCalendar上每隔3天选择一天:

DateTime a = new DateTime();
a = DateTime.Parse(myDatePicker1.Text);

DateTime h = new DateTime();
h = DateTime.Parse(myDatePicker2.Text);

for (DateTime f = a; f < h; f=f.AddDays(3))
{
    MonthlyCalendar.SelectedDates.Add(f);
}

2 个答案:

答案 0 :(得分:0)

我希望这能解决你的问题

 private int cant = 0;

 private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
 {
        if (cant < 3)
        {
            //highlight
            cant++;
        }
        else if (cant < 6)
        {
            //not highlight
            cant++;
        }
        else
        {
            cant = 0;
        }

    }

答案 1 :(得分:0)

这是我的解决方案:

private void btnClick2_Click(object sender, RoutedEventArgs e)
        {
    string dateString1, dateString2, format;
    CultureInfo provider = CultureInfo.InvariantCulture;
            DateTime t = DateTime.Parse(datePicker1.Text);
            DateTime End = DateTime.Parse(datePicker2.Text);
            DateTime g = t.AddDays(6);
            TimeSpan ts = (g - t);

            for (DateTime i = t; i <= End; i += ts)
            {
                DateTime r = i.AddDays(2);    
                dateString1 = i.ToString("MM/dd/yyyy");
                dateString2 = r.ToString("MM/dd/yyyy");
                format = "d";
                DateTime a = DateTime.ParseExact(dateString1, format, provider);
                DateTime b = DateTime.ParseExact(dateString2, format, provider);
                myCalendar.SelectedDates.AddRange(a, b);
            }
        }