您的位置:首页 > 编程语言 > C#

C#编写的基于Silverlight的自定义日历控件

2011-08-10 20:18 573 查看
C#编写的基于Silverlight的自定义日历控件
转载自星月梦缘博客 blog.sina.com.cn/pcwl8521

大家都知道,Silverlight有自己自带的Calendar日历控件,如下图:




Silverlight自带的Calendar日历控件

Calendar控件的命名空间和其他控件一样,都是在System.Windows.Controls下,但是其是在System.Windows.Controls.dll程序集中定义的,所以要引入相应的xml命名空间。
SelectedDatesChanged - 选中日期后所触发的事件

DisplayDateEnd - 此日期之后的日期不予显示

DisplayDateStart - 此日期之前的日期不予显示

FirstDayOfWeek - 控件所显示的每星期的第一天为星期几 [System.DayOfWeek枚举]

DisplayMode - 控件的显示模式 [System.Windows.Controls.DisplayMode枚举]

DisplayMode.Month - 标题显示年月,内容显示日期。默认值

DisplayMode.Year - 标题显示年,内容显示月

DisplayMode.Decade - 标题显示一个十年的区间,内容显示年

IsTodayHighlighted - 是否高亮显示今天的日期

//
Calendar.SelectedDate - 选中的日期

//
Calendar.SelectedDates - 选中的多个日期集合

// Calendar.BlackoutDates - 不允许选择的日期集合

//
Calendar.BlackoutDates.AddDatesInPast() - 禁止选择今天之前的日期

// Calendar.BlackoutDates.Clear() - 清除不允许选择的日期集合 的设置

// CalendarSelectionMode.None - 禁止选择日期

//
CalendarSelectionMode.SingleRange - 可以选择多个日期,连续日期(Shift键配合)

//
CalendarSelectionMode.MultipleRange - 可以选择多个日期,任意日期(Ctrl键配合)

//
CalendarSelectionMode.SingleDate - 只能选择一个日期

总觉得silverlight自带的Calendar不太好控制,而且还不能显示节假日之类的。
在公司现在做的一个项目中要用到日历,觉得silverlight自带的太单调,就自己研究了一个,但是其中还存在一些不足。
下面是我做的一个日历控件,其中包含了阳历、阴历、阳历节日、阴历节日,以及显示了国家法定节假日等信息。如图所示:



C#编写的基于Silverlight的自定义日历控件





这里上面的【10】为阳历日期,下面【十一】为农历日期,



如果该日有节日,如八一建军节,则显示节日;



如果该日的节日名称比较长或者同一天内存在多个节日,如十一国庆节,同时还是世界音乐节、国际老人节等,显示不完整,则显示节日名称前几个;
此外,字体为蓝色的表示国家法定节假日(如国庆节等);



字体为红色的表示周末。





鼠标覆盖日期的时候则显示该日的阳历及其节日信息、阴历及其节日信息等详细信息。
当然,该控件还存在不足的地方,如外观不美观等,还有待增强。
部分代码:

Grid gDay = new Grid();
                    TextBlock txtDay = new TextBlock()
                    {
                        HorizontalAlignment = HorizontalAlignment.Center,
                        FontSize = 18,
                        FontWeight = FontWeights.Bold,
                        Foreground = (SolidColorBrush)App.Current.Resources["biaogezi"],
                        Opacity = 0.9,
                        Margin = new Thickness(0, 1, 0, 19),
                    };
                    TextBlock txtDayTag = new TextBlock()
                    {
                        VerticalAlignment = VerticalAlignment.Bottom,
                        HorizontalAlignment = HorizontalAlignment.Center,
                        FontSize = 10,
                        Height = 12,
                        Foreground = (SolidColorBrush)App.Current.Resources["biaogezi"],
                        Opacity = 0.6,
                        Margin = new Thickness(0, 0, 0, 4),
                    };
                    gDay.Height = 40;
                    gDay.VerticalAlignment = VerticalAlignment.Bottom;
                    gDay.Children.Add(txtDay);
                    gDay.Children.Add(txtDayTag);
                    one = 0;
                    two++;
                    //tmp = year + "-" + month + "-" + i;     //年月日i为日
                    tmp = year + "年" + month + "月" + i + "日";     //年月日i为日
                    string days = GetHappyDays(DateTime.Parse(tmp));
                    aa = ChineseTwentyFourDay(DateTime.Parse(tmp)) + days;
                    if ("".Equals(aa) || aa == null)
                    {
                        if (GetLunarCalendar(DateTime.Parse(tmp)).Substring(2, 2) == "初一")
                        {
                            if (GetLunarCalendar(DateTime.Parse(tmp)).Substring(0, 1) == "闰")
                            { ddday = GetLunarCalendar(DateTime.Parse(tmp)).Substring(0, 2) + "月"; }
                            else
                            { ddday = GetLunarCalendar(DateTime.Parse(tmp)).Substring(0, 2); }
                        }
                        else
                        {
                            ddday = GetLunarCalendar(DateTime.Parse(tmp)).Substring(2, 2);
                        }
                        if (GetLunarCalendar(DateTime.Parse(tmp)).Substring(0, 1) == "闰")
                        { tip = GetLunarCalendar(DateTime.Parse(tmp)).Insert(2, "月"); }
                        else
                        { tip = GetLunarCalendar(DateTime.Parse(tmp)); }
                    }
                    else
                    {
                        ddday = aa;
                        tip = GetLunarCalendar(DateTime.Parse(tmp));
                    }
                    txtDay.Text = i.ToString(); ;
                    txtDayTag.Text = ddday.Length > 5 ? (ddday.Substring(0, 4) + "...") : ddday; ;
                    gDay.SetValue(Grid.ColumnProperty, one);
                    gDay.SetValue(Grid.RowProperty, two);
                    //gDay.SetValue(ToolTipService.ToolTipProperty, tip);
                    gDay.SetValue(ToolTipService.ToolTipProperty, myToolTipService(tmp, tip, aa));

                    if (one == 0 || one == 6)
                    {
                        SolidColorBrush scb = (SolidColorBrush)App.Current.Resources["biaogezi"];
                        txtDay.Foreground = scb;
                        txtDayTag.Foreground = scb;
                    }
                    if (jieri != null && isHappyDay == true)
                    {
                        SolidColorBrush scb = new SolidColorBrush(Colors.Blue);
                        txtDay.Foreground = scb;
                        txtDayTag.Foreground = scb;
                    }
                    gridDate.Children.Add(gDay);
                    Rectangle rectDay = myRectangle(gDay, one, two, tmp);
                    gDay.MouseLeftButtonDown += (obj, e) =>
                    {
                        if (rectDay.Visibility == Visibility.Collapsed)
                        {
                            rectDay.Visibility = Visibility.Visible;
                            DataList.Add(rectDay.Tag.ToString());
                            DataObservableCollection.Add(rectDay.Tag.ToString());
                            dataDateTimeObservableCollection.Add(Convert.ToDateTime(rectDay.Tag.ToString()));
                            //MessageBox.Show("你选的是:" + rectDay.Tag.ToString());
                        }
                        else
                        {
                            rectDay.Visibility = Visibility.Collapsed;
                            DataList.Remove(rectDay.Tag.ToString());
                            DataObservableCollection.Remove(rectDay.Tag.ToString());
                            dataDateTimeObservableCollection.Remove(Convert.ToDateTime(rectDay.Tag.ToString()));
                            //MessageBox.Show("你取消了是:" +rectDay.Tag.ToString() );
                        }
                    };



有兴趣的朋友可以下载:
下载地址如下:

下载Silverlight的自定义日历控件

http://blog.sina.com.cn/s/blog_4ce36a780100wpun.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: