WPF--Calendar控件高级使用
一、得到当前显示的月份:
DateTime SelectedDay = this.MC.DisplayDate;
二、得到当前选中的天,得到当前选中的周,得到当前显示的月份:
如果你使用系统默认的事件SelectedDateChanged是很难获取焦点的,给Calendar注册MC_MouseLeftButtonUp事件
//在初始化时注册事件 MC.AddHandler(Button.MouseLeftButtonDownEvent, new RoutedEventHandler(MC_MouseLeftButtonUp), true); private void MC_MouseLeftButtonUp(object sender, RoutedEventArgs e)
事件的关键代码:
if (sender is Calendar)
{
if (MC.InputHitTest(Mouse.GetPosition(e.Source as FrameworkElement)) is TextBlock)
{
TextBlock tb = MC.InputHitTest(Mouse.GetPosition(e.Source as FrameworkElement))
as TextBlock;//本行代码是个关键,使用了WPF内置的碰撞检测
if (tb != null)
{
try
{
//获取选择的是哪一天
int.Parse(tb.Text);
}
catch (Exception ex) //Click The WeekDaysButton
{
//获取选择的是星期几,如果点击“周1、周2、...周7”按钮,程序会走到此处来得出星期几。
int indexofWeek = (tb.Parent as Grid).Children.IndexOf(tb);
//次方法能获取切换月份按钮后的当前月份
DateTime SelectedDay = this.MC.DisplayDate;
}
}
}
}
三、自定义日期的背景颜色
3.1、首先要自定义几个类,用于模板转换
public class CustemItems
{
bool isSpecific;
public bool IsSpecific
{
get { return isSpecific; }
set { isSpecific = value; }
}
DateTime d;
public DateTime Dete
{
get { return d; }
set { d = value; }
}
string s;
public string StrOfColor
{
get { return s; }
set { s = value; }
}
public CustemItems(DateTime d, string str)
{
Dete = d;
StrOfColor = str;
}
}
public class BlueLetterDayConverter : IValueConverter
{
public static List<CustemItems> dict = new List<CustemItems>();
static BlueLetterDayConverter()
{
}
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
string text = null;
; i < dict.Count; i++)
{
if (dict[i].Dete == (DateTime)value)
{
text = dict[i].StrOfColor;
}
}
return text;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return null;
}
public void Add(DateTime date, string str)
{
dict.Add(new CustemItems(date, str));
}
public static void Update(List<CustemItems> MIList)
{
dict.Clear();
dict = MIList;
}
}
public class RedLetterDayConverter : IValueConverter
{
public static List<CustemItems> dict = new List<CustemItems>();
static RedLetterDayConverter()
{
}
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
string text = null;
; i < dict.Count; i++)
{
if (dict[i].Dete == (DateTime)value)
{
text = dict[i].StrOfColor;
}
}
return text;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return null;
}
public void Add(DateTime date, string str)
{
dict.Add(new CustemItems(date, str));
}
public static void Update(List<CustemItems> MIList)
{
dict.Clear();
dict = MIList;
}
}
3.2 Calendar模板的定义
<Calendar x:Name="MC" HorizontalAlignment="Left" VerticalAlignment="Top"
MouseLeftButtonDown="MC_MouseLeftButtonUp"
>
<Calendar.CalendarDayButtonStyle>
<Style TargetType="{x:Type CalendarDayButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CalendarDayButton}" x:Name="CalendarDayButtonControlTemplate">
<ControlTemplate.Resources>
<src:RedLetterDayConverter x:Key="convRed" />
<src:BlueLetterDayConverter x:Key="convBlue" />
</ControlTemplate.Resources>
<Grid>
<Rectangle x:Name="RedLetterDayBackground" IsHitTestVisible="False" Fill="Red"/>
<Rectangle x:Name="BlueLetterDayConverter" IsHitTestVisible="False" Fill="Blue"/>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="5,1,5,1"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource convRed}}" Value="{x:Null}">
<Setter TargetName="RedLetterDayBackground" Property="Visibility" Value="Hidden" />
</DataTrigger>
<DataTrigger Binding="{Binding Converter={StaticResource convBlue}}" Value="{x:Null}">
<Setter TargetName="BlueLetterDayConverter" Property="Visibility" Value="Hidden" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Calendar.CalendarDayButtonStyle>
</Calendar>
3.3 更新转换模板的List
List<CustemItems> ItemListRed = new List<CustemItems>();
List<CustemItems> ItemListBule = new List<CustemItems>();
...
RedLetterDayConverter.Update(ItemListRed);
BlueLetterDayConverter.Update(ItemListBule);
if (!StartTimer)
{
dispatcherTimer.Start();
}
3.4 重点:
因为从根本上讲ControlTemplate.Resources是一个静态的资源,它无法做到资源变更后,系统自动会通知控件(或者控件模板)去更新对应的UI显示,这个在《WPF程序设计指南[Charles Petzold]》书中的第535页至539页有详细介绍。所以我们要在变更RedLetterDayConverter 或者BlueLetterDayConverter里面的List<>对象以后,需要使用一个Timer去手动更新UI,从而触发模板的更新。
void dispatcherTimer_Tick(object sender, EventArgs e)
{
StartTimer = true;
MC.DisplayDate = MC.DisplayDate.AddMonths();
MC.DisplayDate = MC.DisplayDate.AddMonths(-);
Thread.Sleep();
dispatcherTimer.Stop();
StartTimer = false;
}
完整的例子:
WPF--Calendar控件高级使用的更多相关文章
- WPF常用控件应用demo
WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...
- 【转】WPF - 第三方控件
WPF - 第三方控件 目前第三方控件在网上形成巨大的共享资源,其中包括收费的也有免费的,有开源的也有不开源的,合理的使用第三方控件将使项目组的工作事半功倍.比如项目中有些复杂的业务逻辑.有些绚丽的效 ...
- WPF Popup 控件导致被遮挡内容不刷新的原因
WPF Popup 控件导致被遮挡内容不刷新的原因 周银辉 今天在写一个WPF控件时用到了Popup控件,很郁闷的情况是:当popup关闭时,原来被popup挡住的界面部分不刷新,非要手动刷新一下(比 ...
- 创建 WPF 工具箱控件
创建 WPF 工具箱控件 WPF (Windows Presentation Framework) 工具箱控件模板允许您创建 WPF 控件,会自动添加到 工具箱 安装扩展的安装. 本主题演示如何使用模 ...
- [习题]日历(Calendar)控件的障眼法(.Visible属性),使用时才出现?不用就消失?
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/09/02/calendar_icon_visible.aspx [习题]日历(Cal ...
- wpf打印控件 实现分页打印控件功能
因为 要实现打印 wpf listbox控件 数据特别多 要打印在 几张纸上 找了几天 都没有找到相关的例子 现在 解决了 在这里和大家分享一下 public void print(Fram ...
- asp.net 弹出式日历控件 选择日期 Calendar控件
原文地址:asp.net 弹出式日历控件 选择日期 Calendar控件 作者:逸苡 html代码: <%@ Page Language="C#" CodeFile=&quo ...
- WPF 分页控件 WPF 多线程 BackgroundWorker
WPF 分页控件 WPF 多线程 BackgroundWorker 大家好,好久没有发表一篇像样的博客了,最近的开发实在头疼,很多东西无从下口,需求没完没了,更要命的是公司的开发从来不走正规流程啊, ...
- WPF Image控件中的ImageSource与Bitmap的互相转换
原文:WPF Image控件中的ImageSource与Bitmap的互相转换 1.从bitmap转换成ImageSource [DllImport("gdi32.dll", ...
- ASP.NET Calendar 控件
ASP.NET Calendar 控件 http://www.w3school.com.cn/aspnet/control_calendar.asp
随机推荐
- Redis 设计与实现读书笔记一 Redis字符串
1 Redis 是C语言实现的 2 C字符串是 /0 结束的字符数组 3 Redis具体的动态字符串实现 /* * 保存字符串对象的结构 */ struct sdshdr { // buf 中已占用空 ...
- 微软官方好用的Office 2003、 Office 2007 或 Office 2010 卸载工具
http://support.microsoft.com/kb/2519420/zh-cn
- 如何让你的Apache支持include文件解析和支持shtml的相关配置
源地址:http://www.itokit.com/2011/0430/65992.html Apache支持include文件解析shtml首先要应该修改Apache配置文件httpd.conf . ...
- MapReduce 计算模型
前言 本文讲解Hadoop中的编程及计算模型MapReduce,并将给出在MapReduce模型下编程的基本套路. 模型架构 在Hadoop中,用于执行计算任务(MapReduce任务)的机器有两个角 ...
- UI学习笔记---第九天UITableView表视图
UITableView表视图 一.表视图的使用场景 表视图UITableView是iOS中最重要的视图,随处可见,通常用来管理一组具有相同数据结构的数据 表视图继承自UIScrollView,所以可以 ...
- CentOS云服务器数据盘分区和格式化
1. 查看数据盘信息 登录CentOS云服务器后,可以使用“fdisk -l”命令查看数据盘相关信息. 使用“df –h”命令,无法看到未分区和格式化的数据盘,只能看到已挂载的. [root@VM_7 ...
- Notes of Linked Data concept and application - TODO
Motivation [反正债多了不愁,再开个方向.] Data plays a core role in most business systems, data storage and retrie ...
- js 下载文件 window.location.href
window.location.href ="../../pages2/assessmentplan/exportPointAsessment.do?planId="+planId ...
- 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- 图像处理之face morphing
以前在论坛.微博经常看到一张脸,五官长得像A,脸型似乎又是B,觉得很有意思. 比如像这张图片.这张图片应该是网友用Photoshop完成的,他们取了郭大爷的五官,放在金元帅的脸上,在把边缘处理平滑. ...