什么是WPF最好的自由时间选择器?

我正在为WPF寻找一个简单的时间选择器控件。

  • 我发现找到了这个:

Time Picker

但它有一些问题,例如你不能输入“00”,第二个零不会出现。

  • Silverlight似乎有一个:

http://jesseliberty.com/2009/03/28/toolkit-control-%E2%80%93-timepicker/

但它不适用于WPF

  • WPF工具包本身有一个DatePicker而不是TimePicker。 或者有没有办法允许用户在WPFToolkit DatePicker中输入时间和日期? 它在SelectedDate中返回 DateTime但我没有看到如何允许用户也选择具有此控件的时间。

什么是最好的免费WPF控件,允许用户以HH:MM:SS格式输入时间?

如图所示,您可以轻松自行滚动。 这样,你就能得到你想要的东西。

我喜欢扩展WPF工具包中的控件: 链接到github上的源代码

在包中,包含DateTimeUpDown ,可用于您的目的。 如果您不想使用它的Date部分,可以将Format设置为“ShortTime”或您想要的任何格式。

希望有所帮助。

我在网上找不到一个,所以我从头开始创建它。 我不完全理解依赖属性,所以我现在省略了它们。 该控制是12小时时间选择器控制。 我是WPF的新手,所以我并不完全理解所有新的语言语法,但是这个控件将在我在家创建的项目中为我做好准备。

它支持将时间设置为DateTime或TimeSpan。

下面我将粘贴XAML和Code-Behind。 XAML

                          

守则背后

 using System; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace WorkDayManager3.WPF.UserControls { ///  /// Interaction logic for TimeControl.xaml ///  public partial class TimeControl : UserControl { public TimeControl() { InitializeComponent(); } #region Properties ///  /// Gets or sets the date time value. ///  /// The date time value. public DateTime? DateTimeValue { get { string hours = this.txtHours.Text; string minutes = this.txtMinutes.Text; string amPm = this.txtAmPm.Text; if (!string.IsNullOrWhiteSpace(hours) && !string.IsNullOrWhiteSpace(minutes) && !string.IsNullOrWhiteSpace(amPm)) { string value = string.Format("{0}:{1} {2}", this.txtHours.Text, this.txtMinutes.Text, this.txtAmPm.Text); DateTime time = DateTime.Parse(value); return time; } else { return null; } } set { DateTime? time = value; if (time.HasValue) { string timeString = time.Value.ToShortTimeString(); //9:54 AM string[] values = timeString.Split(':', ' '); if (values.Length == 3) { this.txtHours.Text = values[0]; this.txtMinutes.Text = values[1]; this.txtAmPm.Text = values[2]; } } } } ///  /// Gets or sets the time span value. ///  /// The time span value. public TimeSpan? TimeSpanValue { get { DateTime? time = this.DateTimeValue; if (time.HasValue) { return new TimeSpan(time.Value.Ticks); } else { return null; } } set { TimeSpan? timeSpan = value; if (timeSpan.HasValue) { this.DateTimeValue = new DateTime(timeSpan.Value.Ticks); } } } #endregion #region Event Subscriptions ///  /// Handles the Click event of the btnDown control. ///  /// The source of the event. /// The  instance containing the event data. private void btnDown_Click(object sender, RoutedEventArgs e) { string controlId = this.GetControlWithFocus().Name; if ("txtHours".Equals(controlId)) { this.ChangeHours(false); } else if ("txtMinutes".Equals(controlId)) { this.ChangeMinutes(false); } else if ("txtAmPm".Equals(controlId)) { this.ToggleAmPm(); } } ///  /// Handles the Click event of the btnUp control. ///  /// The source of the event. /// The  instance containing the event data. private void btnUp_Click(object sender, RoutedEventArgs e) { string controlId = this.GetControlWithFocus().Name; if ("txtHours".Equals(controlId)) { this.ChangeHours(true); } else if ("txtMinutes".Equals(controlId)) { this.ChangeMinutes(true); } else if ("txtAmPm".Equals(controlId)) { this.ToggleAmPm(); } } ///  /// Handles the PreviewTextInput event of the txtAmPm control. ///  /// The source of the event. /// The  instance containing the event data. private void txtAmPm_PreviewTextInput(object sender, TextCompositionEventArgs e) { // prevent users to type text e.Handled = true; } ///  /// Handles the KeyUp event of the txt control. ///  /// The source of the event. /// The  instance containing the event data. private void txt_KeyUp(object sender, KeyEventArgs e) { // check for up and down keyboard presses if (Key.Up.Equals(e.Key)) { btnUp_Click(this, null); } else if (Key.Down.Equals(e.Key)) { btnDown_Click(this, null); } } ///  /// Handles the MouseWheel event of the txt control. ///  /// The source of the event. /// The  instance containing the event data. private void txt_MouseWheel(object sender, MouseWheelEventArgs e) { if (e.Delta > 0) { btnUp_Click(this, null); } else { btnDown_Click(this, null); } } ///  /// Handles the PreviewKeyUp event of the txt control. ///  /// The source of the event. /// The  instance containing the event data. private void txt_PreviewKeyUp(object sender, KeyEventArgs e) { TextBox textBox = (TextBox)sender; // make sure all characters are number bool allNumbers = textBox.Text.All(Char.IsNumber); if (!allNumbers) { e.Handled = true; return; } // make sure user did not enter values out of range int value; int.TryParse(textBox.Text, out value); if ("txtHours".Equals(textBox.Name) && value > 12) { EnforceLimits(e, textBox); } else if ("txtMinutes".Equals(textBox.Name) && value > 59) { EnforceLimits(e, textBox); } } #endregion #region Methods ///  /// Changes the hours. ///  /// if set to true [is up]. private void ChangeHours(bool isUp) { int value = Convert.ToInt32(this.txtHours.Text); if (isUp) { value += 1; if (value == 13) { value = 1; } } else { value -= 1; if (value == 0) { value = 12; } } this.txtHours.Text = Convert.ToString(value); } ///  /// Changes the minutes. ///  /// if set to true [is up]. private void ChangeMinutes(bool isUp) { int value = Convert.ToInt32(this.txtMinutes.Text); if (isUp) { value += 1; if (value == 60) { value = 0; } } else { value -= 1; if (value == -1) { value = 59; } } string textValue = Convert.ToString(value); if (value < 10) { textValue = "0" + Convert.ToString(value); } this.txtMinutes.Text = textValue; } ///  /// Enforces the limits. ///  /// The  instance containing the event data. /// The text box. /// The entered value. private static void EnforceLimits(KeyEventArgs e, TextBox textBox) { string enteredValue = GetEnteredValue(e.Key); string text = textBox.Text.Replace(enteredValue, ""); if (string.IsNullOrEmpty(text)) { text = enteredValue; } textBox.Text = text; e.Handled = true; } ///  /// Gets the control with focus. ///  ///  private TextBox GetControlWithFocus() { TextBox txt = new TextBox(); if (this.txtHours.IsFocused) { txt = this.txtHours; } else if (this.txtMinutes.IsFocused) { txt = this.txtMinutes; } else if (this.txtAmPm.IsFocused) { txt = this.txtAmPm; } return txt; } ///  /// Gets the entered value. ///  /// The key. ///  private static string GetEnteredValue(Key key) { string value = string.Empty; switch (key) { case Key.D0: case Key.NumPad0: value = "0"; break; case Key.D1: case Key.NumPad1: value = "1"; break; case Key.D2: case Key.NumPad2: value = "2"; break; case Key.D3: case Key.NumPad3: value = "3"; break; case Key.D4: case Key.NumPad4: value = "4"; break; case Key.D5: case Key.NumPad5: value = "5"; break; case Key.D6: case Key.NumPad6: value = "6"; break; case Key.D7: case Key.NumPad7: value = "7"; break; case Key.D8: case Key.NumPad8: value = "8"; break; case Key.D9: case Key.NumPad9: value = "9"; break; } return value; } ///  /// Toggles the am pm. ///  private void ToggleAmPm() { if ("AM".Equals(this.txtAmPm.Text)) { this.txtAmPm.Text = "PM"; } else { this.txtAmPm.Text = "AM"; } } #endregion } } 

这是控制,随意根据需要进行修改。 它并不完美,但它比我发现的其他控件更好

自版本1.3.0起, MahApps库具有Timepicker控件。