C# 为自定义日历WPF创建事件/约会
我做了一个自定义的时间表,如下所示,它可以正常工作,但现在我正在尝试添加活动/约会,我一辈子都不知道该怎么做。我希望能够单击日历上的一个按钮(日期),创建约会/事件,然后将事件的标题添加到日历上正确的网格单元格(日期)上为其创建的txtblock(网格单元格)中。我不知道如何通过单击指定的按钮来访问某个gridcell。我对这段代码的编写方式并不感到自豪。我本来想拥有一个MVVM,但现在可以了。我不得不面对的问题是,每当我点击<或>按钮更改月份时,一个新的月份被创建,旧的月份被删除。所以一次只做一个月。请帮忙,谢谢 wpf的一部分:使用ItemsControlC# 为自定义日历WPF创建事件/约会,c#,wpf,visual-studio,C#,Wpf,Visual Studio,我做了一个自定义的时间表,如下所示,它可以正常工作,但现在我正在尝试添加活动/约会,我一辈子都不知道该怎么做。我希望能够单击日历上的一个按钮(日期),创建约会/事件,然后将事件的标题添加到日历上正确的网格单元格(日期)上为其创建的txtblock(网格单元格)中。我不知道如何通过单击指定的按钮来访问某个gridcell。我对这段代码的编写方式并不感到自豪。我本来想拥有一个MVVM,但现在可以了。我不得不面对的问题是,每当我点击按钮更改月份时,一个新的月份被创建,旧的月份被删除。所以一次只做一个月
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Content="{Binding day}" Width="175" HorizontalAlignment="Stretch" VerticalAlignment="Top" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Name="btnCalenderDate" Click="btnCalenderDate_Click" Loaded="btnCalenderDate_Loaded" Height="18" FontSize="10" FontWeight="Bold">
</Button>
<TextBlock OpacityMask="Black" Name="txtBlockdays" VerticalAlignment="Top" TextWrapping="Wrap">
</TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<!-- ItemContainerStyle -->
<ItemsControl.ItemContainerStyle>
<Style >
<Setter Property="Grid.Column" Value="{Binding WeekDay}" />
<Setter Property="Grid.Row" Value="{Binding WeekNo}" />
</Style>
</ItemsControl.ItemContainerStyle>
制造日历的类
public partial class SchedulePage : Page, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
MainWindow _parentForm;
Schedule sched = new Schedule();
DateTime date = DateTime.Now;
Event newevent;
public SchedulePage(MainWindow parentForm)
{
InitializeComponent();
_parentForm = parentForm;
DateTime date = DateTime.Now; //These couple of lines determine the current month to show on startup.
_numValue = date.Month;
_numYear = date.Year;
//int year = date.Year;
_nameofmonth = NumValue;
_nameofyear = NumYear;
lblcurrentdate.Content = date.Month + "/ " + date.Day + "/ " + date.Year;
makeCalender();
}
//The region below are properties that are passed in to makeCalender() when the DateRight and DateLeft buttons
// get clicked theses values change. The properties hold the month and the year. Everytime a user
// clicks forward or backward to the next month, these values equal to the selected current month & year.
// However, Everytime these values change a new calender is made and the old one is cleared(deleted).
#region
// properties
public void NotifyPropertyChanged(String info)
{
// if (PropertyChanged != null)
// {
PropertyChanged(this, new PropertyChangedEventArgs(info));
//}
}
private int _numValue;
public int NumValue
{
get { return _numValue; }
set
{
_numValue = value;
}
}
private int _numYear;
public int NumYear
{
get { return _numYear; }
set
{
_numYear = value;
}
}
private int _nameofmonth ;
public int NameofMonth
{
get
{
return this._nameofmonth;
}
set
{
if (value != this._nameofmonth)
{
this._nameofmonth = value;
NotifyPropertyChanged("NameofMonth");
} return;
}
}
private int _nameofyear;
public int NameofYear
{
get
{
return this._nameofyear;
}
set
{
if (value != this._nameofyear)
{
this._nameofyear = value;
NotifyPropertyChanged("NameofYear");
} return;
}
}
#endregion //end of properties
//void TheViewModel_PropertyChanged(object src, PropertyChangedEventArgs e)
/*
{
_parentForm.bindings.schedule.Clear();
_nameofmonth = NumValue;//comboMonth.SelectedIndex + 1;
_nameofyear = comboYear.SelectedIndex + 2011;
makeCalender();
} */
// makeCalender() is how the calender is created and how it determines the gridcell layout of each month.
public void makeCalender() {
var t = new List<Schedule>();
DateTime curr = DateTime.Now;
int[] m = new int[7];
DateTime newcurr = new DateTime(NameofYear, NameofMonth, 1);
var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
var ms = cal.GetWeekOfYear(new DateTime(newcurr.Year, newcurr.Month, 1), System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);
for (int k = 0; k < 7; k++)
{
for (var i = 1; newcurr.Month == NameofMonth; newcurr = newcurr.AddDays(1))
{
var month_week = (newcurr.Day / 7);
sched.MonthWeek = newcurr.GetWeekOfMonth().ToString();
sched.Month = newcurr.Month.ToString();
sched.Year = newcurr.Year.ToString();
sched.day = newcurr.Day.ToString();
sched.WeekOfYear = cal.GetWeekOfYear(newcurr, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString();
sched.dayofweek = newcurr.DayOfWeek.ToString();
t.Add(sched);
//Here is where the calender is created. By looping through all the days in the selected month it will find the weeknumber and day of the week -->
// that that particular date belongs to and place in the correct gridcell.
_parentForm.bindings.schedule.Add(new Schedule { WeekNo = newcurr.GetWeekOfMonth() - 1, WeekDay = (int)newcurr.DayOfWeek, day = newcurr.Day.ToString() });
lblDate.Content = getMonth(newcurr.Month) + " " + newcurr.Year; //getMonth() determines string format of current month displayed.
}
}
DataContext = _parentForm.bindings;
}
// btnDateRight_Click allows us to navigate forward to the next month, if current month is -->
// december then the current year is incremented and the month starts on january (NumValue = 01)
private void btnDateRight_Click(object sender, RoutedEventArgs e)
{
_parentForm.bindings.schedule.Clear();
if (NumValue >= 1 && NumValue < 12)
{
NumValue += 1;
_nameofmonth = NumValue;
} else
{
NumYear += 1;
_nameofyear = NumYear;
NumValue = 01;
_nameofmonth = NumValue;
}
makeCalender();
}
// btnDateLeft_Click allows us navigate backwards to a previous month
// if navigating back and the current month is january, then the year is deincremented the current month is -->
// set to December (NumValue = 12)
private void btnDateLeft_Click(object sender, RoutedEventArgs e)
{
_parentForm.bindings.schedule.Clear();
if (NumValue > 1 && NumValue <= 12)
{
NumValue -= 1;
_nameofmonth = NumValue;
}
else
{
NumYear -= 1;
_nameofyear = NumYear;
NumValue = 12;
_nameofmonth = NumValue;
}
makeCalender();
}
// btnCalenderDate_Click (as of 11/5/2011) will only return the current month, day, year in messagebox
// --> Problem is displaying an event on the textblock(gridcell) of the selected button(date). -->
// Example: If I click on the button 5 of november 2011(11/5/2011) I should be able to make an event
// and display the title of the event on that pictular gridcell of 11/5/2011.
private void btnCalenderDate_Click(object sender, RoutedEventArgs e)
{
Button b = e.Source as Button;
int day = Convert.ToInt32(b.Content);
MessageBox.Show(NumValue + "/" + b.Content + "/" + NumYear);
//b.Content = "123";
newevent = new Event(NumValue, day, NumYear);
newevent.Show();
}
//btnCalenderDate_Loaded will determine todays date and highlight the button.
private void btnCalenderDate_Loaded(object sender, RoutedEventArgs e) //Sets the current day on the calender
{
DateTime date = DateTime.Now;
Button today = e.Source as Button;
if (NumValue == date.Month && (String)today.Content == date.Day.ToString() && NumYear == date.Year)
{
today.Background = Brushes.Aqua;
}
}
公共部分类SchedulePage:Page,INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
主窗口\u父窗体;
Schedule sched=新的Schedule();
DateTime date=DateTime.Now;
事件新事件;
公共计划页(主窗口父窗体)
{
初始化组件();
_parentForm=parentForm;
DateTime date=DateTime.Now;//这两行决定了启动时要显示的当前月份。
_numValue=日期。月份;
_numYear=日期。年份;
//int year=日期。年份;
_nameofmonth=NumValue;
_name of year=NumYear;
lblcurrentdate.Content=date.Month+“/”+date.Day+“/”+date.Year;
制作日历();
}
//下面的区域是当使用DateRight和DateLeft按钮时传递给MakeCalendar()的属性
//单击这些值会更改。这些属性包含月份和年份。每次用户
//单击“向前”或“向后”到下一个月,这些值等于所选的当前月份和年份。
//但是,每次更改这些值时,都会创建一个新日历,并清除(删除)旧日历。
#区域
//性质
public void NotifyPropertyChanged(字符串信息)
{
//if(PropertyChanged!=null)
// {
PropertyChanged(此,新PropertyChangedEventArgs(信息));
//}
}
私有整数价值;
公共整数数值
{
获取{return\u numValue;}
设置
{
_numValue=值;
}
}
私人国际博物馆;
纽迈尔公共酒店
{
获取{return\u numYear;}
设置
{
_numYear=数值;
}
}
私人国际单位名称月;
公共整数月名
{
得到
{
返回此项。\u name of month;
}
设置
{
if(值!=此._nameofmonth)
{
这是。_nameof month=值;
NotifyPropertyChanged(“月名”);
}返回;
}
}
私人国际单位年度名称;
公共国际年度名称
{
得到
{
返回此项。_年度名称;
}
设置
{
如果(值!=本年的名称)
{
这个。_nameof year=值;
通知财产变更(“年度名称”);
}返回;
}
}
#endregion//属性的末尾
//取消ViewModel_PropertyChanged(对象src、PropertyChangedEventArgs e)
/*
{
_parentForm.bindings.schedule.Clear();
_nameofmonth=NumValue;//comboMonth.SelectedIndex+1;
_nameofyear=comboYear.SelectedIndex+2011;
制作日历();
} */
//MakeCalendar()是日历的创建方式,以及它如何确定每个月的gridcell布局。
公共日历(){
var t=新列表();
DateTime curr=DateTime.Now;
int[]m=新的int[7];
DateTime newcurr=新日期时间(年份名称、月份名称,1);
var cal=System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
var ms=cal.GetWeekOfYear(新日期时间(newcurr.Year,newcurr.Month,1),System.Globalization.CalendarWeekRule.FirstDay,System.DayOfWeek.Sunday);
对于(int k=0;k<7;k++)
{
对于(变量i=1;新货币月份==NameofMonth;新货币=newcurr.AddDays(1))
{
var月\周=(新货币日/7);
sched.MonthWeek=newcurr.GetWeekOfMonth().ToString();
sched.Month=newcurr.Month.ToString();
sched.Year=newcurr.Year.ToString();
sched.day=newcurr.day.ToString();
sched.WeekOfYear=cal.GetWeekOfYear(newcurr,System.Globalization.CalendarWeekRule.FirstDay,DayOfWeek.Sunday).ToString();
sched.dayofweek=newcurr.dayofweek.ToString();
t、 添加(附表);
//这里是创建日历的地方。通过循环选定月份中的所有日期,它将找到周数和一周中的日期-->
//该特定日期属于并放置在正确的网格单元格中。
_parentForm.bindings.schedule.Add(新计划{WeekNo=newcurr.getweekofmount()-1,WeekDay=(int)newcurr.DayOfWeek,day=newcurr.day.ToString());
lblDate.Content=getMonth(newcurr.Month)+“”+newcurr.Year;//getMonth()确定