最近有一个项目中,因为数据库查询导出的问题,要频繁用到选择日期时间时间的工具,而WPF自带的控件中只有Datepicker这个控件,不足以满足功能,鉴于WPF强大的自定义控件Usercontrol的功能,我自己就简单制作了一个日期时间控件---DateTimePicker。------姜彦 20170804
先给展示一下这个控件的简单情况:
思路是利用WPF自带的Calendar 做日历,然后自己再制作一个时间选择的控件,最后是把两者做到一个控件上,形成一个最终控件DateTimePicker,可是实现直接调用,或者以后再项目中直接添加,或者通过调用dll的方式使用。
控件的工程文件如下:
为了方便以后调用,我创建的是一个 WPF用户控件库,这个类型的类库,可以添加Usercontrol控件,并能最后编译生成dll。
一、控件的制作
1.DateTimePicker
是控件的最终显示的窗体设计跟属性设计的文件
DateTimePicker.xaml文件
View
1 <UserControl x:Class="Utility.Tool.Controls.View.DateTimePicker" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 mc:Ignorable="d" 7 d:DesignHeight="25" 8 d:DesignWidth="150" 9 xmlns:my="clr-namespace:Utility.Tool.Controls.View" 10 Loaded="UserControl_Loaded" 11 MaxWidth="150" 12 MaxHeight="25" 13 14 > 15 16 <Grid Height="25" Width="150"> 17 18 <Border BorderBrush="Silver" 19 BorderThickness="1" HorizontalAlignment="Left" 20 Margin="0,0,0,0" 21 Name="border1" 22 Width="150" 23 Height="25" 24 VerticalAlignment="Top" 25 26 > 27 28 <my:IconButton 29 x:Name="iconButton1" 30 Height="18" 31 Width="19" 32 HorizontalAlignment="Right" 33 Icon="/Utility.Tool.Controls;component/Image/date.png" 34 Click="iconButton1_Click" 35 36 /> 37 38 </Border> 39 40 <TextBlock 41 Height="23" 42 HorizontalAlignment="Left" 43 Margin="5,5,0,0" 44 Name="textBlock1" 45 VerticalAlignment="Top" 46 Text="2017/07/31 18:19:20" 47 Width="123" 48 49 /> 50 51 <Grid x:Name="girdChioce" 52 Background="Transparent" 53 VerticalAlignment="Top" 54 Margin="0,258,0,40"> 55 <Popup x:Name="popChioce" 56 PopupAnimation="Fade" 57 PlacementTarget="{Binding ElementName=girdChioce}" 58 Placement="Top" 59 AllowsTransparency="True" 60 StaysOpen="False" 61 IsOpen="False"> 62 </Popup> 63 </Grid> 64 65 </Grid> 66 </UserControl>