2015-12-03 66 views
2

在我的應用程序中,我有一個TextBox,用戶可以在其中輸入格式爲HH:mm:ss的時間值。TextBox for Time

TextBox的XAML看起來像:

<TextBox xMinWidth="60" HorizontalAlignment="Left" Background="Transparent"> 
    <TextBox.Text> 
     <Binding Path="FileTime" UpdateSourceTrigger="PropertyChanged" StringFormat="T" ConverterCulture="de-DE" />   
    </TextBox.Text> 
</TextBox> 

酒店FileTime在視圖模型是DateTime類型。

如果我嘗試輸入值13:15:45 TextBox顯示我13:15:04此刻我鍵入4.如果我鍵入最後5個,則在TextBox中的結果是13:15: 045這不是一個有效的時間。

如何讓我的TextBox接受一個數字作爲第二個,並且不會附加前導零?

+3

http://wpftoolkit.codeplex.com/wikipage?title=TimePicker&referringTitle=Documentation ? – Dennis

+0

你爲什麼不嘗試改變StringFormat? –

+0

我已經嘗試將StringFormat更改爲HH:mm:ss - >我有同樣的情況。如果我將它改爲HH:mm:s,那麼比用戶鍵入13:14:5的時間長,並且離開輸入區的秒數將保持爲5.在這種情況下,我希望第二個變成05 – Tomtom

回答

1

我發現了一個適合我的完美解決方案,每個TextBox都可以輕鬆重複使用。

我寫了一個AttachedProperty它看起來像:

public class TextBoxExtensions 
{ 
    public static readonly DependencyProperty EditStringFormatProperty = DependencyProperty.RegisterAttached(
     "EditStringFormat", typeof (string), typeof (TextBoxExtensions), 
     new PropertyMetadata(default(string), OnEditStringFormatChanged)); 

    private static readonly DependencyProperty OriginalBindingExpressionProperty = DependencyProperty 
     .RegisterAttached(
      "OriginalBindingExpression", typeof (BindingExpression), typeof (TextBoxExtensions), 
      new PropertyMetadata(default(BindingExpression))); 

    private static void OnEditStringFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     TextBox textBox = d as TextBox; 
     if (textBox == null) 
      return; 

     if (e.NewValue != null && e.OldValue == null) 
     { 
      textBox.IsKeyboardFocusedChanged += TextBoxOnIsKeyboardFocusedChanged; 
     } 
     else if (e.OldValue != null && e.NewValue == null) 
     { 
      textBox.IsKeyboardFocusedChanged -= TextBoxOnIsKeyboardFocusedChanged; 
     } 
    } 

    private static void TextBoxOnIsKeyboardFocusedChanged(object sender, DependencyPropertyChangedEventArgs e) 
    { 
     TextBox textBox = sender as TextBox; 

     if (textBox == null) 
      return; 

     if (GetOriginalBindingExpression(textBox) == null) 
     { 
      SetOriginalBindingExpression(textBox, textBox.GetBindingExpression(TextBox.TextProperty)); 
     } 

     BindingExpression bindingExpression = GetOriginalBindingExpression(textBox); 

     if (textBox.IsKeyboardFocused) 
     { 
      Binding parentBinding = bindingExpression.ParentBinding; 
      Binding newBinding = new Binding(parentBinding.Path.Path) 
      { 
       ElementName = parentBinding.ElementName, 
       Path = parentBinding.Path, 
       Mode = parentBinding.Mode, 
       UpdateSourceTrigger = parentBinding.UpdateSourceTrigger, 
       StringFormat = "H:m:s" 
      }; 
      foreach (ValidationRule validationRule in parentBinding.ValidationRules) 
      { 
       newBinding.ValidationRules.Add(validationRule); 
      } 
      textBox.SetBinding(TextBox.TextProperty, newBinding); 
     } 
     else 
     { 
      textBox.SetBinding(TextBox.TextProperty, bindingExpression.ParentBinding); 
     } 
    } 

    public static void SetEditStringFormat(DependencyObject element, string value) 
    { 
     element.SetValue(EditStringFormatProperty, value); 
    } 

    public static string GetEditStringFormat(DependencyObject element) 
    { 
     return (string) element.GetValue(EditStringFormatProperty); 
    } 

    private static void SetOriginalBindingExpression(DependencyObject element, BindingExpression value) 
    { 
     element.SetValue(OriginalBindingExpressionProperty, value); 
    } 

    private static BindingExpression GetOriginalBindingExpression(DependencyObject element) 
    { 
     return (BindingExpression) element.GetValue(OriginalBindingExpressionProperty); 
    } 
} 

和使用就是:

<TextBox x:Name="TxtHour" MinWidth="60" HorizontalAlignment="Left" MaxLength="8" Background="Transparent" 
     Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}" 
     attachedProperties:TextBoxExtensions.EditStringFormat="H:m:s"> 
    <TextBox.Text> 
     <Binding Path="FileTime" UpdateSourceTrigger="PropertyChanged" StringFormat="HH:mm:ss"> 
      <Binding.ValidationRules> 
       <validationRules:StringIsTimeValidationRule /> 
      </Binding.ValidationRules> 
     </Binding> 
    </TextBox.Text> 
</TextBox>