2009-12-02 15 views
17

मैं एक साधारण समय पिकर WPF के लिए नियंत्रण की तलाश में हूं।वर्तमान में WPF के लिए सबसे अच्छा, खाली समय पिकर क्या है?

  • मैं इस एक पाया पाया है:

http://marlongrech.wordpress.com/2007/11/18/time-picker/

लेकिन यह कुछ मुद्दों उदा है आप इसमें "00" टाइप नहीं कर सकते हैं, दूसरा शून्य दिखाई नहीं देगा।

  • सिल्वरलाइट एक है लगता है:

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

लेकिन यह WPF के लिए नहीं है।

  • डब्ल्यूपीएफ टूलकिट में डेटपिकर है लेकिन टाइमपिकर स्वयं ही नहीं है। या क्या उपयोगकर्ता को WPFToolkit DatePicker में समय और दिनांक दर्ज करने की अनुमति देने का कोई तरीका है? यह चयनित दिनांक में डेटटाइम देता है लेकिन मुझे नहीं लगता कि उपयोगकर्ता को इस नियंत्रण के साथ समय चुनने की अनुमति कैसे दी जाए।

उपयोगकर्ताओं को एचएच में समय दर्ज करने की अनुमति देने के लिए सबसे अच्छा मुफ्त WPF नियंत्रण क्या है: एमएम: एसएस प्रारूप?

उत्तर

3

here दिखाए गए अनुसार आप अपना खुद का सुंदर आसानी से रोल कर सकते हैं। और इस तरह, आप वही प्राप्त कर सकते हैं जो आप चाहते हैं।

+0

यह एक अच्छा उदाहरण है, मैं बस कोड बदलने के बिना कीबोर्ड और माउस इनपुट लेगा। :-) – wonea

+0

समय पिकर भयानक है – CRice

5

मुझे वेब पर कोई नहीं मिला, इसलिए मैंने इसे स्क्रैच से बनाया। मैं निर्भरता गुणों को पूरी तरह से समझ नहीं पा रहा हूं, इसलिए अब मैं उनको छोड़ दूंगा। नियंत्रण एक 12 घंटे का समय पिकर नियंत्रण है। मैं डब्ल्यूपीएफ के लिए नया हूं इसलिए मैं सभी नए भाषा वाक्यविन्यासों को पूरी तरह से समझ नहीं पा रहा हूं लेकिन यह नियंत्रण मेरे द्वारा बनाई गई परियोजना में मेरे लिए चाल करेगा।

यह डेटटाइम या टाइमस्पेन के रूप में सेटिंग समय का समर्थन करता है।

नीचे मैं एक्सएएमएल और कोड-बेहिंद पेस्ट कर दूंगा। XAML

<UserControl x:Class="WorkDayManager3.WPF.UserControls.TimeControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="35" d:DesignWidth="100"> 
    <Border BorderBrush="LightBlue" BorderThickness="1" Margin="1"> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition /> 
       <ColumnDefinition Width="5" /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
       <ColumnDefinition /> 
      </Grid.ColumnDefinitions> 
      <TextBox x:Name="txtHours" BorderThickness="0" MaxLength="2" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Text="1" KeyUp="txt_KeyUp" MouseWheel="txt_MouseWheel" PreviewKeyUp="txt_PreviewKeyUp" /> 
      <TextBlock Text=":" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
      <TextBox x:Name="txtMinutes" BorderThickness="0" MaxLength="2" TextAlignment="Center" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" Text="00" KeyUp="txt_KeyUp" MouseWheel="txt_MouseWheel" PreviewKeyUp="txt_PreviewKeyUp" /> 
      <TextBox x:Name="txtAmPm" BorderThickness="0" MaxLength="2" TextAlignment="Center" Grid.Column="3" HorizontalAlignment="Left" VerticalAlignment="Center" PreviewTextInput="txtAmPm_PreviewTextInput" Text="AM" KeyUp="txt_KeyUp" MouseWheel="txt_MouseWheel" Padding="0, 0, 3, 0" /> 
      <Grid Grid.Column="4"> 
       <Grid.RowDefinitions> 
        <RowDefinition /> 
        <RowDefinition /> 
       </Grid.RowDefinitions> 
       <Button x:Name="btnUp" Focusable="False" Click="btnUp_Click"> 
        <TextBlock Text="p" FontFamily="Wingdings 3" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
       </Button> 
       <Button x:Name="btnDown" Grid.Row="1" Focusable="False" Click="btnDown_Click"> 
        <TextBlock Text="q" FontFamily="Wingdings 3" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
       </Button> 
      </Grid> 
     </Grid> 
    </Border> 
</UserControl> 

कोड-पीछे

using System; 
using System.Linq; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 

namespace WorkDayManager3.WPF.UserControls 
{ 
    /// <summary> 
    /// Interaction logic for TimeControl.xaml 
    /// </summary> 
    public partial class TimeControl : UserControl 
    { 
     public TimeControl() 
     { 
      InitializeComponent(); 
     } 

     #region Properties 

     /// <summary> 
     /// Gets or sets the date time value. 
     /// </summary> 
     /// <value>The date time value.</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]; 
        } 
       } 
      } 
     } 

     /// <summary> 
     /// Gets or sets the time span value. 
     /// </summary> 
     /// <value>The time span value.</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 

     /// <summary> 
     /// Handles the Click event of the btnDown control. 
     /// </summary> 
     /// <param name="sender">The source of the event.</param> 
     /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param> 
     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(); 
      } 
     } 

     /// <summary> 
     /// Handles the Click event of the btnUp control. 
     /// </summary> 
     /// <param name="sender">The source of the event.</param> 
     /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param> 
     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(); 
      } 
     } 

     /// <summary> 
     /// Handles the PreviewTextInput event of the txtAmPm control. 
     /// </summary> 
     /// <param name="sender">The source of the event.</param> 
     /// <param name="e">The <see cref="System.Windows.Input.TextCompositionEventArgs"/> instance containing the event data.</param> 
     private void txtAmPm_PreviewTextInput(object sender, TextCompositionEventArgs e) 
     { 
      // prevent users to type text 
      e.Handled = true; 
     } 

     /// <summary> 
     /// Handles the KeyUp event of the txt control. 
     /// </summary> 
     /// <param name="sender">The source of the event.</param> 
     /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param> 
     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); 
      } 
     } 

     /// <summary> 
     /// Handles the MouseWheel event of the txt control. 
     /// </summary> 
     /// <param name="sender">The source of the event.</param> 
     /// <param name="e">The <see cref="System.Windows.Input.MouseWheelEventArgs"/> instance containing the event data.</param> 
     private void txt_MouseWheel(object sender, MouseWheelEventArgs e) 
     { 
      if (e.Delta > 0) 
      { 
       btnUp_Click(this, null); 
      } 
      else 
      { 
       btnDown_Click(this, null); 
      } 
     } 

     /// <summary> 
     /// Handles the PreviewKeyUp event of the txt control. 
     /// </summary> 
     /// <param name="sender">The source of the event.</param> 
     /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param> 
     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 

     /// <summary> 
     /// Changes the hours. 
     /// </summary> 
     /// <param name="isUp">if set to <c>true</c> [is up].</param> 
     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); 
     } 

     /// <summary> 
     /// Changes the minutes. 
     /// </summary> 
     /// <param name="isUp">if set to <c>true</c> [is up].</param> 
     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; 
     } 

     /// <summary> 
     /// Enforces the limits. 
     /// </summary> 
     /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param> 
     /// <param name="textBox">The text box.</param> 
     /// <param name="enteredValue">The entered value.</param> 
     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; 
     } 

     /// <summary> 
     /// Gets the control with focus. 
     /// </summary> 
     /// <returns></returns> 
     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; 
     } 

     /// <summary> 
     /// Gets the entered value. 
     /// </summary> 
     /// <param name="key">The key.</param> 
     /// <returns></returns> 
     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; 
     } 

     /// <summary> 
     /// Toggles the am pm. 
     /// </summary> 
     private void ToggleAmPm() 
     { 
      if ("AM".Equals(this.txtAmPm.Text)) 
      { 
       this.txtAmPm.Text = "PM"; 
      } 
      else 
      { 
       this.txtAmPm.Text = "AM"; 
      } 
     } 

     #endregion 
    } 
} 

नियंत्रण है यही कारण है, आवश्यकतानुसार संशोधन करें के लिए स्वतंत्र महसूस। यह सही नहीं है, लेकिन यह मैं

23

पाया है मैं विस्तारित WPF टूलकिट से नियंत्रण चाहते अन्य नियंत्रण की तुलना में बेहतर है: DateTimeUpDown निहित है जो अपने उद्देश्य के लिए इस्तेमाल किया जा सकता है Link to source code on codeplex

पैकेज में,। यदि आप Date भाग का उपयोग नहीं करना चाहते हैं, तो आप Format को "शॉर्टटाइम" या जो भी प्रारूप चाहते हैं उसे सेट कर सकते हैं।

उम्मीद है कि मदद करता है।

+1

मैंने उन्हें बहुत उपयोगी पाया। धन्यवाद! –

+0

मुझे खेद है ... मुझे नहीं पता कि क्यों वे डेटटाइम का उपयोग करते हैं? टाइमपिकर के मूल्य के लिए ... जो मेरे मॉडल के साथ फिट नहीं है। शायद मैं इसे ठीक करने के लिए चीजें कर सकता हूं लेकिन मैं दूसरे नियंत्रण में जाना पसंद करता हूं। –

+1

वे ऐसा करते हैं ताकि जब आप डेटाबेस तालिका कॉलम से जुड़ जाएंगे, तब उस नियंत्रण में फिट नहीं होगा जब उस कॉलम में नल होते हैं, जैसा कि वे अक्सर करते हैं। यदि आपका मॉडल इस परिदृश्य पर विचार नहीं करता है तो शायद इसे और अधिक विचार की आवश्यकता है। –

संबंधित मुद्दे