我正在寻找一个控件(UWP或ASP.NET),它给我一个日历年视图(财政年度;-))并让我对日期进行分类。所以我希望能够点击一天,然后选择一种表示忙碌,可用的颜色......或者我怎样才能创建一些能够实现此功能的东西?
目前我用它来计划未来的日子。 在excel中看起来像这样。
答案 0 :(得分:0)
要更改Background
的{{1}},我们应该可以使用CalendarViewDayItem
属性在CalendarViewDayItemChangingEventArgs.Item
事件中获取CalendarViewDayItem
。
我们应该能够创建一个具有MyColor和MyDateTime属性的类来保存CalendarViewDayItemChanging
和Color
。
例如:
DateTime
背后的代码:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<CalendarView Name="MyCalendarView" SelectedDatesChanged="MyCalendarView_SelectedDatesChanged" CalendarViewDayItemChanging="MyCalendarView_CalendarViewDayItemChanging"></CalendarView>
</Grid>
当我们设置public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
times = new ObservableCollection<DayItem>();
for (int i = 1; i < 10; i++)
{
times.Add(new DayItem(new DateTime(2017, 1, i), Colors.Yellow));
}
for (int i = 10; i < 20; i++)
{
times.Add(new DayItem(new DateTime(2016, 12, i), Colors.Green));
}
for (int i = 20; i < 29; i++)
{
times.Add(new DayItem(new DateTime(2017, 1, i), Colors.Red));
}
}
private ObservableCollection<DayItem> times;
private void MyCalendarView_SelectedDatesChanged(CalendarView sender, CalendarViewSelectedDatesChangedEventArgs args)
{
bool add = false;
//If we double click the CalendarViewDayItem, the second time args.AddedDates.Count is 0
if (args.AddedDates.Count >= 1)
{
var newDate = args.AddedDates[0];
for (int i = 0; i < times.Count; i++)
{
if (newDate.Date.Year == times[i].MyDateTime.Year && newDate.Date.Month == times[i].MyDateTime.Month && newDate.Date.Day == times[i].MyDateTime.Day)
{
add = true;
}
}
if (!add)
{
times.Add(new DayItem(newDate.DateTime, Colors.Green));
}
}
}
private void MyCalendarView_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
{
var selectDay = args.Item as CalendarViewDayItem;
for (int i = 0; i < times.Count; i++)
{
if (selectDay.Date.Year == times[i].MyDateTime.Year && selectDay.Date.Month == times[i].MyDateTime.Month && selectDay.Date.Day == times[i].MyDateTime.Day)
selectDay.Background = new SolidColorBrush(times[i].MyColor);
}
}
}
internal class DayItem
{
public DayItem(DateTime today, Color red)
{
MyDateTime = today;
MyColor = red;
}
public Color MyColor { get; set; }
public DateTime MyDateTime { get; set; }
}
的{{1}}时,Background
将不会被解雇。它在加载CalendarViewDayItem时发生。所以你应该可以重新加载CalendarViewDayItem。
此外,如果您想要显示日历年度视图,您应该可以在页面上使用12个CalendarView。