Calendar控件在工具箱中有,但是奇怪的是在SDK中并没有相关说明,也许是我本地安装有问题。我联想ASP.NET和WindowsForm中Calendar控件的相关用法,简单试验了一下。
下面试验实现获取选择的日期值,和使Calendar显示指定的日期值。
XAML代码中用到了Canvas布局。
<
UserControl
xmlns:basics
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
x:Class
="_51CTO.lesson02.Calendar"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
Width ="400" Height ="300" >
< Canvas Background ="LightSteelBlue" >
< basics:Calendar Name ="Calendar1" Canvas.Top ="0" Canvas.Left ="0"
SelectedDatesChanged ="Calendar_SelectedDatesChanged" > </ basics:Calendar >
< TextBox Name ="SelectedDate" Canvas.Top ="170" Canvas.Left ="0" Width ="100" > </ TextBox >
< Button Name ="SetDate" Content ="设置" Canvas.Top ="170" Canvas.Left ="110" Width ="50" Click ="Button_Click" > </ Button >
</ Canvas >
</ UserControl >
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
Width ="400" Height ="300" >
< Canvas Background ="LightSteelBlue" >
< basics:Calendar Name ="Calendar1" Canvas.Top ="0" Canvas.Left ="0"
SelectedDatesChanged ="Calendar_SelectedDatesChanged" > </ basics:Calendar >
< TextBox Name ="SelectedDate" Canvas.Top ="170" Canvas.Left ="0" Width ="100" > </ TextBox >
< Button Name ="SetDate" Content ="设置" Canvas.Top ="170" Canvas.Left ="110" Width ="50" Click ="Button_Click" > </ Button >
</ Canvas >
</ UserControl >
C#代码如下,分别就选择日期和设置按钮点击实现:
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
![InBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/d608a01840fefd0e145dfeac03617766.png)
效果图如下:
![](https://i-blog.csdnimg.cn/blog_migrate/dd53b56dcb25b95ebb4acf8fe69ce2f4.png)
点击设置按钮可以根据文本框中输入的日期来切换Calendar显示。
本文转自 王杰瑞 51CTO博客,原文链接:http://blog.51cto.com/wangjierui/118027,如需转载请自行联系原作者