Csharp/C#教程:什么是WPF最好的自由时间选择器?分享


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

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

Time Picker

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

Toolkit Control – TimePicker

但它不适用于WPF

什么是最好的免费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控件。

上述就是C#学习教程:什么是WPF最好的自由时间选择器?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/982929.html

(0)
上一篇 2021年12月18日
下一篇 2021年12月18日

精彩推荐