最近有一个项目中,因为数据库查询导出的问题,要频繁用到选择日期时间时间的工具,而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>

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 using System.Drawing; 15 16 namespace Utility.Tool.Controls.View 17 { 18 19 [ToolboxBitmap(typeof(DateTimePicker), "DateTimePicker.bmp")] 20 /// <summary> 21 /// DateTimePicker.xaml 的交互逻辑 22 /// </summary> 23 public partial class DateTimePicker : UserControl 24 { 25 public DateTimePicker() 26 { 27 InitializeComponent(); 28 } 29 30 /// <summary> 31 /// 构造函数 32 /// </summary> 33 /// <param name="txt"></param> 34 public DateTimePicker(string txt) 35 : this() 36 { 37 // this.textBox1.Text = txt; 38 39 } 40 41 #region 事件 42 43 /// <summary> 44 /// 日历图标点击事件 45 /// </summary> 46 /// <param name="sender"></param> 47 /// <param name="e"></param> 48 private void iconButton1_Click(object sender, RoutedEventArgs e) 49 { 50 if (popChioce.IsOpen == true) 51 { 52 popChioce.IsOpen = false; 53 } 54 55 TDateTimeView dtView = new TDateTimeView(textBlock1.Text);// TDateTimeView 构造函数传入日期时间 56 dtView.DateTimeOK += (dateTimeStr) => //TDateTimeView 日期时间确定事件 57 { 58 59 textBlock1.Text = dateTimeStr; 60 DateTime = Convert.ToDateTime(dateTimeStr); 61 popChioce.IsOpen = false;//TDateTimeView 所在pop 关闭 62 63 }; 64 65 popChioce.Child = dtView; 66 popChioce.IsOpen = true; 67 } 68 69 /// <summary> 70 /// DateTimePicker 窗体登录事件 71 /// </summary> 72 /// <param name="sender"></param> 73 /// <param name="e"></param> 74 private void UserControl_Loaded(object sender, RoutedEventArgs e) 75 { 76 DateTime dt = DateTime.Now; 77 textBlock1.Text = dt.ToString("yyyy/MM/dd HH:mm:ss");//"yyyyMMddHHmmss" 78 DateTime = dt; 79 // DateTime = Convert.ToDateTime(textBlock1.Text); 80 } 81 82 #endregion 83 84 #region 属性 85 86 /// <summary> 87 /// 日期时间 88 /// </summary> 89 public DateTime DateTime { get; set; } 90 91 #endregion 92 } 93 }
2.IconButton
是控件中使用到的一个类button的按钮,可以自定义图片的按钮
View

1 <UserControl x:Class="Utility.Tool.Controls.View.IconButton" 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="32" 8 d:DesignWidth="32" 9 10 > 11 <UserControl.Resources> 12 <Style x:Key="ButtonEmptyStyle" TargetType="{x:Type Button}"> 13 <Setter Property="HorizontalContentAlignment" Value="Left"/> 14 <Setter Property="HorizontalAlignment" Value="Stretch"/> 15 <Setter Property="Template"> 16 <Setter.Value> 17 <ControlTemplate TargetType="{x:Type Button}"> 18 <Border x:Name="ButtonBorder" 19 CornerRadius="3" 20 BorderThickness="1" 21 SnapsToDevicePixels="True" 22 BorderBrush="Transparent" 23 Background="{TemplateBinding Background}" 24 Margin="0"> 25 <Grid> 26 <ContentPresenter/> 27 </Grid> 28 </Border> 29 <ControlTemplate.Triggers> 30 <Trigger Property="IsPressed" Value="true"> 31 <Setter TargetName="ButtonBorder" 32 Property="Opacity" 33 Value="0.5"> 34 </Setter> 35 </Trigger> 36 </ControlTemplate.Triggers> 37 </ControlTemplate> 38 </Setter.Value> 39 </Setter> 40 </Style> 41 </UserControl.Resources> 42 43 <Button x:Name="button" 44 Background="Transparent" 45 Style="{StaticResource ButtonEmptyStyle}" 46 Padding="0" 47 Focusable="False" 48 FocusVisualStyle="{x:Null}"> 49 <!--ContextMenu="{StaticResource EmerWorkContextMenu}"--> 50 <Grid> 51 <Image x:Name="icon"/> 52 </Grid> 53 </Button> 54 </UserControl>

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 15 namespace Utility.Tool.Controls.View 16 { 17 18 [System.ComponentModel.DesignTimeVisible(false)]//在工具箱中 隐藏该窗体 20170804 姜彦 19 public partial class IconButton : UserControl 20 { 21 public IconButton() 22 { 23 InitializeComponent(); 24 25 this.button.Click += delegate 26 { 27 RoutedEventArgs newEvent = new RoutedEventArgs(IconButton.ClickEvent, this); 28 this.RaiseEvent(newEvent); 29 }; 30 } 31 32 #region 图标 33 public static readonly DependencyProperty IconProperty = 34 DependencyProperty.Register("Icon", 35 typeof(string), 36 typeof(IconButton), 37 new PropertyMetadata(string.Empty, OnIconChanged)); 38 public string Icon 39 { 40 set { SetValue(IconProperty, value); } 41 get { return (string)GetValue(IconProperty); } 42 } 43 private static void OnIconChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) 44 { 45 IconButton btn = obj as IconButton; 46 if (btn == null) 47 { 48 return; 49 } 50 btn.icon.Source = new BitmapImage(new Uri((string)args.NewValue, UriKind.Relative)); 51 } 52 #endregion 53 54 #region 命令 55 56 public static readonly DependencyProperty CommandProperty = 57 DependencyProperty.Register("Command", 58 typeof(ICommand), 59 typeof(IconButton), 60 new PropertyMetadata(null, OnSelectCommandChanged)); 61 public ICommand Command 62 { 63 set { SetValue(CommandProperty, value); } 64 get { return (ICommand)GetValue(CommandProperty); } 65 } 66 private static void OnSelectCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) 67 { 68 IconButton btn = obj as IconButton; 69 if (btn == null) 70 { 71 return; 72 } 73 btn.button.Command = (ICommand)args.NewValue; 74 } 75 76 #endregion 77 78 #region 点击事件 79 public static readonly RoutedEvent ClickEvent = 80 EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, 81 typeof(RoutedEventHandler), typeof(IconButton)); 82 83 public event RoutedEventHandler Click 84 { 85 add 86 { 87 base.AddHandler(ClickEvent, value); 88 } 89 remove 90 { 91 base.RemoveHandler(ClickEvent, value); 92 } 93 94 } 95 #endregion 96 97 } 98 }
3.TDateTimeView
是控件中当点击后的主View,呈现所有的日期跟时间等相关信息的。
View

1 <UserControl x:Class="Utility.Tool.Controls.View.TDateTimeView" 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="252" 8 d:DesignWidth="235" 9 xmlns:my="clr-namespace:Utility.Tool.Controls.View" 10 Loaded="UserControl_Loaded" 11 12 > 13 14 <UserControl.Resources> 15 16 <Style x:Key="CommonDataGridStyle" TargetType="DataGrid"> 17 <!--网格线颜色--> 18 <Setter Property="Background" Value="#829db2" /> 19 <Setter Property="BorderBrush" Value="#00c5d6e6" /> 20 <Setter Property="HorizontalGridLinesBrush"> 21 <Setter.Value> 22 <SolidColorBrush Color="#d2d2d2"/> 23 </Setter.Value> 24 </Setter> 25 <Setter Property="VerticalGridLinesBrush"> 26 <Setter.Value> 27 <SolidColorBrush Color="#d2d2d2"/> 28 </Setter.Value> 29 </Setter> 30 </Style> 31 32 <Style x:Key="CommonDataGridColumnHeaderStyle" TargetType="DataGridColumnHeader"> 33 <Setter Property="SnapsToDevicePixels" Value="True" /> 34 <Setter Property="MinWidth" Value="0" /> 35 <Setter Property="MinHeight" Value="28" /> 36 <Setter Property="Foreground" Value="#ffffff" /> 37 <Setter Property="FontSize" Value="12" /> 38 <Setter Property="Cursor" Value="Hand" /> 39 <Setter Property="Height" Value="25"/> 40 <Setter Property="Template"> 41 <Setter.Value> 42 <ControlTemplate TargetType="DataGridColumnHeader"> 43 <Border x:Name="BackgroundBorder" BorderThickness="0,0,0,1" 44 BorderBrush="#c1d8e8" Width="Auto"> 45 <Grid > 46 <Grid.ColumnDefinitions> 47 <ColumnDefinition Width="*" /> 48 </Grid.ColumnDefinitions> 49 <ContentPresenter Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center"/> 50 <Path x:Name="SortArrow" Visibility="Collapsed" 51 Data="M0,0 L1,0 0.5,1 z" 52 Stretch="Fill" 53 Grid.Column="2" Width="8" Height="6" 54 Fill="White" 55 Margin="0,0,50,0" 56 VerticalAlignment="Center" 57 RenderTransformOrigin="1,1" /> 58 <Rectangle Width="1" Fill="#c1d8e8" 59 HorizontalAlignment="Right" 60 Grid.ColumnSpan="1"/> 61 <!--<TextBlock Background="Red"> 62 <ContentPresenter></ContentPresenter></TextBlock>--> 63 </Grid> 64 </Border> 65 </ControlTemplate> 66 </Setter.Value> 67 </Setter> 68 </Style> 69 70 <Style x:Key="CommonDataGridRowStyle" TargetType="DataGridRow"> 71 <Setter Property="Background" Value="#F2F2F2" /> 72 <Setter Property="Foreground" Value="Black" /> 73 <Style.Triggers> 74 <!--隔行换色--> 75 <Trigger Property="AlternationIndex" Value="0" > 76 <Setter Property="Background" Value="#ffffff" /> 77 </Trigger> 78 <Trigger Property="AlternationIndex" Value="1" > 79 <Setter Property="Background" Value="#ffffff" /> 80 </Trigger> 81 <!--<Trigger Property="IsMouseOver" Value="True"> 82 <Setter Property="Background" Value="LightGray"/> 83 </Trigger>--> 84 <Trigger Property="IsSelected" Value="True"> 85 <Setter Property="Background" Value="#ffffff"/> 86 <Setter Property="Foreground" Value="Black"/> 87 </Trigger> 88 </Style.Triggers> 89 </Style> 90 91 <Style x:Key="CommonButtonStyle" TargetType="{x:Type Button}"> 92 <Setter Property="FontSize" Value="12"/> 93 <Setter Property="Foreground" Value="White"/> 94 <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 95 <Setter Property="Template"> 96 <Setter.Value> 97 <ControlTemplate TargetType="{x:Type Button}"> 98 <Border x:Name="ButtonBorder" 99 CornerRadius="3" 100 BorderThickness="1" 101 SnapsToDevicePixels="True" 102 Background="{TemplateBinding Background}" 103 Margin="0"> 104 <Grid> 105 <ContentPresenter HorizontalAlignment="Center" 106 VerticalAlignment="Center" 107 Margin="0,0,0,0" /> 108 </Grid> 109 </Border> 110 <ControlTemplate.Triggers> 111 <Trigger Property="IsPressed" Value="true"> 112 <Setter TargetName="ButtonBorder" 113 Property="Opacity" 114 Value="0.5"> 115 </Setter> 116 </Trigger> 117 <Trigger Property="IsEnabled" Value="false"> 118 <Setter TargetName="ButtonBorder" 119 Property="Background" 120 Value="#d8d8d8"> 121 </Setter> 122 </Trigger> 123 </ControlTemplate.Triggers> 124 </ControlTemplate> 125 </Setter.Value> 126 </Setter> 127 </Style> 128 129 </UserControl.Resources> 130 131 <Border BorderBrush="#FF93C2F8" 132 BorderThickness="1" 133 > 134 135 <Grid Height="229" 136 Width="185" 137 Background="White" 138 139 > 140 141 <Grid.RowDefinitions> 142 <RowDefinition Height="24" /> 143 <RowDefinition Height="186*" /> 144 <RowDefinition Height="39" /> 145 146 </Grid.RowDefinitions> 147 148 <Grid Grid.Row="0" 149 Background="#FFAFCCF8" 150 151 > 152 <TextBlock x:Name="tbTitle" 153 Text="日期时间" 154 VerticalAlignment="Center" 155 FontSize="12" 156 Margin="70,0,0,0" 157 Foreground="White"/> 158 159 <my:IconButton 160 HorizontalAlignment="Left" 161 Margin="165,3,0,0" 162 x:Name="iBtnCloseView" 163 VerticalAlignment="Top" 164 Height="18" 165 Width="18" 166 Icon="/Utility.Tool.Controls;component/Image/close.png" 167 Click="iBtnCloseView_Click" /> 168 169 </Grid> 170 171 <Grid Grid.Row="1"> 172 <Calendar Name="calDate" 173 Height="165" 174 Width="186" 175 Margin="0,0,0,0" 176 VerticalAlignment="Top" PreviewMouseUp="calDate_PreviewMouseUp" /> 177 178 </Grid> 179 180 <Grid Grid.Row="2" Margin="0,0,0,6"> 181 182 <Border x:Name="TDateTime2" 183 BorderBrush="#FFA6D4F8" 184 185 BorderThickness="1" 186 Margin="35,-1,84,0" 187 Height="23" > 188 189 </Border> 190 191 192 <Label Name="lblTime" 193 Content="时间" 194 Height="28" 195 HorizontalAlignment="Left" 196 Margin="2,0,0,0" 197 198 /> 199 200 <TextBlock Name="textBlockhh" 201 Height="23" 202 Text="18" 203 PreviewMouseLeftButtonDown="textBlockhh_PreviewMouseLeftButtonDown" 204 Margin="40,8,0,6" 205 HorizontalAlignment="Left" 206 Width="15" 207 /> 208 209 <TextBlock Name="textBlockh" 210 Height="23" 211 HorizontalAlignment="Left" 212 Margin="55,8,0,6" 213 Text=":" 214 /> 215 216 <TextBlock Name="textBlockmm" 217 Height="23" 218 HorizontalAlignment="Left" 219 Margin="60,8,0,6" 220 Text="19" 221 PreviewMouseLeftButtonDown="textBlockmm_PreviewMouseLeftButtonDown" 222 223 /> 224 225 <TextBlock Name="textBlockm" 226 Height="23" 227 HorizontalAlignment="Left" 228 Margin="75,8,0,6" 229 Text=":" 230 231 /> 232 233 <TextBlock Name="textBlockss" 234 Height="23" 235 HorizontalAlignment="Left" 236 Margin="80,8,0,7" 237 Text="20" PreviewMouseLeftButtonDown="textBlockss_PreviewMouseLeftButtonDown" /> 238 239 <Button Name="btnNow" 240 Content="当前" 241 Height="23" 242 HorizontalAlignment="Left" 243 Margin="105,0,0,6" 244 VerticalAlignment="Bottom" 245 Width="36" 246 Style="{StaticResource CommonButtonStyle}" 247 Background="LightBlue" 248 Foreground="White" 249 Click="btnNow_Click" 250 /> 251 252 <Button Name="btnOK" 253 Content="确定" 254 Height="23" 255 HorizontalAlignment="Left" 256 Margin="143,0,0,6" 257 VerticalAlignment="Bottom" 258 Width="39" 259 Style="{StaticResource CommonButtonStyle}" 260 Background="#ecc158" 261 Foreground="White" Click="btnOK_Click" /> 262 263 </Grid> 264 265 <Grid x:Name="girdChioce" 266 Background="Transparent" 267 VerticalAlignment="Top" 268 Margin="5,188,0,40"> 269 <Popup x:Name="popChioce" 270 PopupAnimation="Fade" 271 PlacementTarget="{Binding ElementName=girdChioce}" 272 Placement="Top" 273 AllowsTransparency="True" 274 StaysOpen="False" 275 IsOpen="False"> 276 </Popup> 277 </Grid> 278 279 280 </Grid> 281 282 </Border> 283 284 </UserControl>

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 using System.Windows.Controls.Primitives; 15 16 namespace Utility.Tool.Controls.View 17 { 18 [System.ComponentModel.DesignTimeVisible(false)]//在工具箱中 隐藏该窗体 20170804 姜彦 19 /// <summary> 20 /// TDateTime.xaml 的交互逻辑 21 /// </summary> 22 public partial class TDateTimeView : UserControl 23 { 24 public TDateTimeView() 25 { 26 InitializeComponent(); 27 } 28 29 /// <summary> 30 /// 构造函数 31 /// </summary> 32 /// <param name="txt"></param> 33 public TDateTimeView(string txt) 34 : this() 35 { 36 this.formerDateTimeStr = txt; 37 } 38 39 #region 全局变量 40 41 /// <summary> 42 /// 从 DateTimePicker 传入的日期时间字符串 43 /// </summary> 44 private string formerDateTimeStr = string.Empty; 45 46 // private string selectDate = string.Empty; 47 48 #endregion 49 50 #region 事件 51 52 /// <summary> 53 /// TDateTimeView 窗体登录事件 54 /// </summary> 55 /// <param name="sender"></param> 56 /// <param name="e"></param> 57 private void UserControl_Loaded(object sender, RoutedEventArgs e) 58 { 59 //当前时间 60 //DateTime dt = Convert.ToDateTime(this.formerDateTimeStr); 61 //textBlockhh.Text = dt.Hour.ToString().PadLeft(2,'0'); 62 //textBlockmm.Text = dt.Minute.ToString().PadLeft(2, '0'); 63 //textBlockss.Text = dt.Second.ToString().PadLeft(2, '0'); 64 65 //00:00:00 66 textBlockhh.Text = "00"; 67 textBlockmm.Text = "00"; 68 textBlockss.Text = "00"; 69 } 70 71 72 /// <summary> 73 /// 关闭按钮事件 74 /// </summary> 75 /// <param name="sender"></param> 76 /// <param name="e"></param> 77 private void iBtnCloseView_Click(object sender, RoutedEventArgs e) 78 { 79 OnDateTimeContent(this.formerDateTimeStr); 80 } 81 82 /// <summary> 83 /// 确定按钮事件 84 /// </summary> 85 /// <param name="sender"></param> 86 /// <param name="e"></param> 87 private void btnOK_Click(object sender, RoutedEventArgs e) 88 { 89 DateTime? dt = new DateTime?(); 90 91 if (calDate.SelectedDate == null) 92 { 93 dt = DateTime.Now.Date; 94 } 95 else 96 { 97 dt = calDate.SelectedDate; 98 } 99 100 DateTime dtCal = Convert.ToDateTime(dt); 101 102 string timeStr = "00:00:00"; 103 timeStr = textBlockhh.Text + ":" + textBlockmm.Text + ":" + textBlockss.Text; 104 105 string dateStr; 106 dateStr = dtCal.ToString("yyyy/MM/dd"); 107 108 string dateTimeStr; 109 dateTimeStr = dateStr + " " + timeStr; 110 111 string str1 = string.Empty; ; 112 str1 = dateTimeStr; 113 OnDateTimeContent(str1); 114 115 } 116 117 /// <summary> 118 /// 当前按钮事件 119 /// </summary> 120 /// <param name="sender"></param> 121 /// <param name="e"></param> 122 private void btnNow_Click(object sender, RoutedEventArgs e) 123 { 124 popChioce.IsOpen = false;//THourView 或 TMinSexView 所在pop 的关闭动作 125 126 if (btnNow.Content == "零点") 127 { 128 textBlockhh.Text = "00"; 129 textBlockmm.Text = "00"; 130 textBlockss.Text = "00"; 131 btnNow.Content = "当前"; 132 btnNow.Background = System.Windows.Media.Brushes.LightBlue; 133 } 134 else 135 { 136 DateTime dt = DateTime.Now; 137 textBlockhh.Text = dt.Hour.ToString().PadLeft(2, '0'); 138 textBlockmm.Text = dt.Minute.ToString().PadLeft(2, '0'); 139 textBlockss.Text = dt.Second.ToString().PadLeft(2, '0'); 140 btnNow.Content = "零点"; 141 btnNow.Background = System.Windows.Media.Brushes.LightGreen; 142 } 143 144 145 } 146 147 /// <summary> 148 /// 小时点击事件 149 /// </summary> 150 /// <param name="sender"></param> 151 /// <param name="e"></param> 152 private void textBlockhh_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 153 { 154 155 if (popChioce.IsOpen == true) 156 { 157 popChioce.IsOpen = false; 158 } 159 160 THourView hourView = new THourView(textBlockhh.Text);// THourView 构造函数传递小时数据 161 hourView.HourClick += (hourstr) => //THourView 点击所选小时后的 传递动作 162 { 163 164 textBlockhh.Text = hourstr; 165 popChioce.IsOpen = false;//THourView 所在pop 的关闭动作 166 }; 167 168 popChioce.Child = hourView; 169 popChioce.IsOpen = true; 170 171 172 //View 退出事件 姜彦20170306 173 //HG.ViewClose += (Flag) => 174 //{ 175 // popChioce.IsOpen = false; 176 177 //}; 178 } 179 180 /// <summary> 181 /// 分钟点击事件 182 /// </summary> 183 /// <param name="sender"></param> 184 /// <param name="e"></param> 185 private void textBlockmm_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 186 { 187 188 if (popChioce.IsOpen == true) 189 { 190 popChioce.IsOpen = false; 191 } 192 193 TMinSexView minView = new TMinSexView(textBlockmm.Text);//TMinSexView 构造函数传递 分钟数据 194 minView.MinClick += (minStr) => //TMinSexView 中 点击选择的分钟数据的 传递动作 195 { 196 197 textBlockmm.Text = minStr; 198 popChioce.IsOpen = false;//TMinSexView 所在的 pop 关闭动作 199 }; 200 201 popChioce.Child = minView; 202 popChioce.IsOpen = true; 203 } 204 205 /// <summary> 206 /// 秒钟点击事件 207 /// </summary> 208 /// <param name="sender"></param> 209 /// <param name="e"></param> 210 private void textBlockss_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 211 { 212 if (popChioce.IsOpen == true) 213 { 214 popChioce.IsOpen = false; 215 } 216 217 //秒钟 跟分钟 都是60,所有秒钟共用 分钟的窗体即可 218 TMinSexView sexView = new TMinSexView(textBlockss.Text);//TMinSexView 构造函数 传入秒钟数据 219 sexView.textBlockTitle.Text = "秒 钟";//修改 TMinSexView 的标题名称为秒钟 220 sexView.MinClick += (sexStr) => //TMinSexView 中 所选择确定的 秒钟数据 的传递动作 221 { 222 textBlockss.Text = sexStr; 223 popChioce.IsOpen = false;//TMinSexView 所在的 pop 关闭动作 224 }; 225 226 popChioce.Child = sexView; 227 popChioce.IsOpen = true; 228 } 229 230 231 /// <summary> 232 /// 解除calendar点击后 影响其他按钮首次点击无效的问题 233 /// </summary> 234 /// <param name="sender"></param> 235 /// <param name="e"></param> 236 private void calDate_PreviewMouseUp(object sender, MouseButtonEventArgs e) 237 { 238 if (Mouse.Captured is CalendarItem) 239 { 240 Mouse.Capture(null); 241 } 242 } 243 244 245 #endregion 246 247 #region Action交互 248 249 /// <summary> 250 /// 时间确定后的传递事件 251 /// </summary> 252 public Action<string> DateTimeOK; 253 254 /// <summary> 255 /// 时间确定后传递的时间内容 256 /// </summary> 257 /// <param name="dateTimeStr"></param> 258 protected void OnDateTimeContent(string dateTimeStr) 259 { 260 if (DateTimeOK != null) 261 DateTimeOK(dateTimeStr); 262 } 263 264 #endregion 265 266 267 268 269 270 271 } 272 }
4.THourView
是提供小时选择界面
View

1 <UserControl x:Class="Utility.Tool.Controls.View.THourView" 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="188" 8 d:DesignWidth="200" 9 xmlns:my="clr-namespace:Utility.Tool.Controls.View" 10 11 Loaded="UserControl_Loaded" 12 > 13 <Border BorderBrush="#FF88C1F2" 14 BorderThickness="2" 15 > 16 17 <Grid> 18 19 <Grid.RowDefinitions> 20 <RowDefinition Height="24"/> 21 <RowDefinition Height="*"/> 22 </Grid.RowDefinitions> 23 24 <Grid Grid.Row="0" 25 Background="#FFAFCCF8" 26 > 27 28 <TextBlock x:Name="tbTitle" 29 Text="小 时" 30 FontSize="12" 31 Margin="40,2,0,0" 32 Foreground="White" 33 /> 34 35 <my:IconButton 36 HorizontalAlignment="Right" 37 Margin="0,2,3,0" 38 x:Name="iBtnCloseView" 39 VerticalAlignment="Top" 40 Height="18" 41 Width="18" 42 Icon="/Utility.Tool.Controls;component/Image/close.png" 43 Click="iBtnCloseView_Click" /> 44 45 </Grid> 46 47 <DataGrid Grid.Row="1" 48 AutoGenerateColumns="True" 49 Name="dgHour" 50 SelectionMode="Single" 51 AlternationCount="2" 52 RowHeaderWidth="0" 53 CanUserAddRows="False" 54 VerticalAlignment="Top" 55 RowHeight="18" 56 ColumnWidth="18" 57 FontSize="11" 58 SelectionUnit="Cell" 59 IsReadOnly="True" 60 HeadersVisibility="Row" 61 62 63 Background="White" 64 BorderBrush="White" 65 HorizontalGridLinesBrush="#d2d2d2" 66 VerticalGridLinesBrush="#d2d2d2" 67 SelectedCellsChanged="dgHour_SelectedCellsChanged"> 68 69 /> 70 71 </DataGrid> 72 73 74 75 76 </Grid> 77 78 </Border> 79 80 </UserControl>

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 15 namespace Utility.Tool.Controls.View 16 { 17 [System.ComponentModel.DesignTimeVisible(false)]//在工具箱中 隐藏该窗体 20170804 姜彦 18 /// <summary> 19 /// THourGrid.xaml 的交互逻辑 20 /// </summary> 21 public partial class THourView : UserControl 22 { 23 public THourView() 24 { 25 InitializeComponent(); 26 } 27 28 /// <summary> 29 /// 构造函数 30 /// </summary> 31 /// <param name="txt"></param> 32 public THourView(string txt) 33 : this() 34 { 35 this.formerHourStr = txt; 36 } 37 38 #region 全局变量 39 40 /// <summary> 41 /// 从 TDateTimeView 传入的 小时数据 字符串 42 /// </summary> 43 private string formerHourStr = string.Empty; 44 45 #endregion 46 47 #region 类 48 49 /// <summary> 50 /// 类:小时数据 51 /// </summary> 52 public class Hour 53 { 54 /// <summary> 55 /// 第1列 小时数据 56 /// </summary> 57 public int Hour1 { get; set; } 58 public int Hour2 { get; set; } 59 public int Hour3 { get; set; } 60 public int Hour4 { get; set; } 61 public int Hour5 { get; set; } 62 63 /// <summary> 64 /// 第6列 小时数据 65 /// </summary> 66 public int Hour6 { get; set; } 67 68 /// <summary> 69 /// 构造函数 70 /// </summary> 71 /// <param name="hour1"></param> 72 /// <param name="hour2"></param> 73 /// <param name="hour3"></param> 74 /// <param name="hour4"></param> 75 /// <param name="hour5"></param> 76 /// <param name="hour6"></param> 77 public Hour(int hour1, int hour2, int hour3, int hour4, int hour5, int hour6) 78 { 79 Hour1 = hour1; 80 Hour2 = hour2; 81 Hour3 = hour3; 82 Hour4 = hour4; 83 Hour5 = hour5; 84 Hour6 = hour6; 85 86 } 87 88 } 89 90 #endregion 91 92 #region 方法 93 94 /// <summary> 95 /// dgHour控件 绑定类Hour 加载初始化数据 96 /// </summary> 97 public void LoadHour() 98 { 99 Hour[] hour = new Hour[4]; 100 101 hour[0] = new Hour(0, 1, 2, 3, 4, 5); 102 hour[1] = new Hour(6, 7, 8, 9, 10, 11); 103 hour[2] = new Hour(12, 13, 14, 15, 16, 17); 104 hour[3] = new Hour(18, 19, 20, 21, 22, 23); 105 106 dgHour.Items.Clear(); 107 dgHour.ItemsSource = hour; 108 109 } 110 111 #endregion 112 113 #region 事件 114 115 /// <summary> 116 /// THourView 窗体登录事件 117 /// </summary> 118 /// <param name="sender"></param> 119 /// <param name="e"></param> 120 private void UserControl_Loaded(object sender, RoutedEventArgs e) 121 { 122 LoadHour(); 123 } 124 125 /// <summary> 126 /// dgHour控件 单元格点击(选择)事件 127 /// </summary> 128 /// <param name="sender"></param> 129 /// <param name="e"></param> 130 private void dgHour_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) 131 { 132 DataGridCellInfo cell = dgHour.CurrentCell; 133 if (cell.Column == null) 134 { 135 return; 136 } 137 138 Hour hour = cell.Item as Hour; 139 140 // string str = cell.Column.DisplayIndex.ToString(); 141 142 string str1 = string.Empty; ; 143 switch (cell.Column.DisplayIndex)// 通过所在列 获取类Hour的坐标 确定具体的hour数据 144 { 145 case 0: 146 str1 = hour.Hour1.ToString(); 147 break; 148 149 case 1: 150 str1 = hour.Hour2.ToString(); 151 break; 152 153 case 2: 154 str1 = hour.Hour3.ToString(); 155 break; 156 157 case 3: 158 str1 = hour.Hour4.ToString(); 159 break; 160 161 case 4: 162 str1 = hour.Hour5.ToString(); 163 break; 164 165 case 5: 166 str1 = hour.Hour6.ToString(); 167 break; 168 169 default: break; 170 } 171 172 str1 = str1.PadLeft(2, '0'); 173 OnHourClickContentEdit(str1); 174 175 } 176 177 /// <summary> 178 /// 关闭按钮事件 179 /// </summary> 180 /// <param name="sender"></param> 181 /// <param name="e"></param> 182 private void iBtnCloseView_Click(object sender, RoutedEventArgs e) 183 { 184 OnHourClickContentEdit(this.formerHourStr); 185 } 186 187 #endregion 188 189 #region Action 交互 190 191 /// <summary> 192 /// 小时数据点击(确定)后 的传递事件 193 /// </summary> 194 public Action<string> HourClick; 195 196 /// <summary> 197 /// 小时数据点击(确定)后 传递的时间内容 198 /// </summary> 199 /// <param name="hourstr"></param> 200 protected void OnHourClickContentEdit(string hourstr) 201 { 202 if (HourClick != null) 203 HourClick(hourstr); 204 } 205 206 #endregion 207 208 209 210 211 212 213 214 215 216 } 217 }
5.TMinSexView
提供分钟跟秒钟选择的界面
View

1 <UserControl x:Class="Utility.Tool.Controls.View.TMinSexView" 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="180" 8 d:DesignWidth="240" 9 Loaded="UserControl_Loaded" 10 11 xmlns:my="clr-namespace:Utility.Tool.Controls.View" 12 > 13 14 <Border BorderBrush="#FF88C1F2" 15 BorderThickness="2" 16 17 18 > 19 20 <Grid> 21 22 <Grid.RowDefinitions> 23 <RowDefinition Height="20"/> 24 <RowDefinition Height="*"/> 25 </Grid.RowDefinitions> 26 27 <Grid Grid.Row="0" 28 Background="#FFAFCCF8" 29 30 > 31 <TextBlock x:Name="textBlockTitle" 32 Text="分 钟" 33 FontSize="12" 34 Margin="0,2,1,0" 35 Foreground="White" HorizontalAlignment="Right" Width="145" /> 36 37 <my:IconButton 38 Margin="0,0,1,0" 39 x:Name="iBtnCloseView" 40 VerticalAlignment="Top" 41 HorizontalAlignment="Right" 42 Width="18" 43 Height="18" 44 Icon="/Utility.Tool.Controls;component/Image/close.png" 45 Click="iBtnCloseView_Click" 46 /> 47 48 </Grid> 49 50 <DataGrid Grid.Row="1" 51 AutoGenerateColumns="True" 52 Name="dgMinSex" 53 SelectionMode="Single" 54 AlternationCount="1" 55 RowHeaderWidth="0" 56 CanUserAddRows="False" 57 VerticalAlignment="Top" 58 RowHeight="18" 59 ColumnWidth="18" 60 FontSize="11" 61 SelectionUnit="Cell" 62 IsReadOnly="True" 63 HeadersVisibility="Row" 64 65 Background="White" 66 BorderBrush="White" 67 HorizontalGridLinesBrush="#d2d2d2" 68 VerticalGridLinesBrush="#d2d2d2" SelectedCellsChanged="dgMinSex_SelectedCellsChanged"> 69 70 /> 71 72 </DataGrid> 73 74 75 76 77 </Grid> 78 79 </Border> 80 81 82 </UserControl>

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 15 namespace Utility.Tool.Controls.View 16 { 17 [System.ComponentModel.DesignTimeVisible(false)]//在工具箱中 隐藏该窗体 20170804 姜彦 18 /// <summary> 19 /// TMinSexView.xaml 的交互逻辑 20 /// </summary> 21 public partial class TMinSexView : UserControl 22 { 23 public TMinSexView() 24 { 25 InitializeComponent(); 26 } 27 28 /// <summary> 29 /// 构造函数 30 /// </summary> 31 /// <param name="txt"></param> 32 public TMinSexView(string txt) 33 : this() 34 { 35 this.formerMinStr = txt; 36 } 37 38 #region 全局变量 39 40 /// <summary> 41 /// 从 TDateTimeView 传入的 分钟数据 字符串 42 /// </summary> 43 public string formerMinStr = string.Empty; 44 45 #endregion 46 47 #region 类 48 49 /// <summary> 50 /// 类:分钟数据 51 /// </summary> 52 public class Min 53 { 54 public int C0 { get; set; } 55 public int C1 { get; set; } 56 public int C2 { get; set; } 57 public int C3 { get; set; } 58 public int C4 { get; set; } 59 public int C5 { get; set; } 60 public int C6 { get; set; } 61 public int C7 { get; set; } 62 public int C8 { get; set; } 63 public int C9 { get; set; } 64 65 66 67 68 public Min(int c0, int c1, int c2, int c3, int c4, int c5, int c6, int c7, int c8, int c9) 69 { 70 C0 = c0; 71 C1 = c1; 72 C2 = c2; 73 C3 = c3; 74 C4 = c4; 75 C5 = c5; 76 C6 = c6; 77 C7 = c7; 78 C8 = c8; 79 C9 = c9; 80 81 82 } 83 84 } 85 86 #endregion 87 88 #region 方法 89 90 /// <summary> 91 /// dgMinSex控件 绑定类Min 加载初始化数据 92 /// </summary> 93 public void LoadMin() 94 { 95 Min[] min = new Min[6]; 96 97 min[0] = new Min(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); 98 min[1] = new Min(10, 11, 12, 13, 14, 15, 16, 17, 18, 19); 99 min[2] = new Min(20, 21, 22, 23, 24, 25, 26, 27, 28, 29); 100 min[3] = new Min(30, 31, 32, 33, 34, 35, 36, 37, 38, 39); 101 min[4] = new Min(40, 41, 42, 43, 44, 45, 46, 47, 48, 49); 102 min[5] = new Min(50, 51, 52, 53, 54, 55, 56, 57, 58, 59); 103 104 dgMinSex.Items.Clear(); 105 dgMinSex.ItemsSource = min; 106 107 } 108 109 #endregion 110 111 #region 事件 112 113 /// <summary> 114 /// TMinSexView 窗体登录事件 115 /// </summary> 116 /// <param name="sender"></param> 117 /// <param name="e"></param> 118 private void UserControl_Loaded(object sender, RoutedEventArgs e) 119 { 120 LoadMin(); 121 } 122 123 /// <summary> 124 /// dgMinSex控件 单元格点击(选择)事件 125 /// </summary> 126 /// <param name="sender"></param> 127 /// <param name="e"></param> 128 private void dgMinSex_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) 129 { 130 131 DataGridCellInfo cell = dgMinSex.CurrentCell; 132 if (cell.Column == null) 133 { 134 return; 135 } 136 137 Min min = cell.Item as Min; 138 139 //string str = cell.Column.DisplayIndex.ToString(); 140 141 string str1 = string.Empty; ; 142 switch (cell.Column.DisplayIndex)// 通过所在列 获取类Min的坐标 确定具体的min数据 143 { 144 case 0: 145 str1 = min.C0.ToString(); 146 break; 147 148 case 1: 149 str1 = min.C1.ToString(); 150 break; 151 152 case 2: 153 str1 = min.C2.ToString(); 154 break; 155 156 case 3: 157 str1 = min.C3.ToString(); 158 break; 159 160 case 4: 161 str1 = min.C4.ToString(); 162 break; 163 164 case 5: 165 str1 = min.C5.ToString(); 166 break; 167 168 case 6: 169 str1 = min.C6.ToString(); 170 break; 171 172 case 7: 173 str1 = min.C7.ToString(); 174 break; 175 176 case 8: 177 str1 = min.C8.ToString(); 178 break; 179 180 case 9: 181 str1 = min.C9.ToString(); 182 break; 183 184 default: break; 185 186 } 187 188 str1 = str1.PadLeft(2, '0'); 189 OnMinClickContent(str1); 190 } 191 192 /// <summary> 193 /// 窗体关闭事件 194 /// </summary> 195 /// <param name="sender"></param> 196 /// <param name="e"></param> 197 private void iBtnCloseView_Click(object sender, RoutedEventArgs e) 198 { 199 OnMinClickContent(this.formerMinStr); 200 } 201 202 #endregion 203 204 #region Action 交互 205 206 /// <summary> 207 /// 分钟数据点击(确定)后 的传递事件 208 /// </summary> 209 public Action<string> MinClick; 210 211 /// <summary> 212 /// 分钟数据点击(确定)后 传递的时间内容 213 /// </summary> 214 /// <param name="minStr"></param> 215 protected void OnMinClickContent(string minStr) 216 { 217 if (MinClick != null) 218 MinClick(minStr); 219 } 220 221 #endregion 222 223 224 225 226 227 228 229 230 } 231 }
二、控件工具箱图标的制作
1.在引用里面添加 System.Drawing;
2.可以下载一个图标,也可以自己制作一个图标,bmp或ico格式的,必须是16*16位的。
3.添加或制作后,右键,点击属性,修改生成操作为:嵌入的资源
4.路径必须跟控件所在的命名空间地址一致 ,参照解决方案图片里面看到位图文件就在View里面
5.在代码的起始位置,写入关键的一句话:[ToolboxBitmap(typeof(DateTimePicker), "DateTimePicker.bmp")]
三、控件的添加
1.项目添加(图标为默认图标)
在解决方案中,直接 添加-现有项--选择该项目文件即可,然后控件就会出现。
2.dll直接引用
首先,将 Utility.Tool.Controls.dll 文件放到你工程文件的debug文件夹里面;
然后,在 工具箱 面板中 右键 添加选项卡-新命名一下;
最后,在新的选项卡里面 右键-选择项-WPF组件-Utility.Tool.Controls.dll所在的文件夹-选择该dll-确定,即可
下载地址:http://download.csdn.net/detail/jiangyan2008521/9921183
https://github.com/jiangyan219/DateTimePicker.git