我發現了一個適合我的完美解決方案,每個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>
http://wpftoolkit.codeplex.com/wikipage?title=TimePicker&referringTitle=Documentation ? – Dennis
你爲什麼不嘗試改變StringFormat? –
我已經嘗試將StringFormat更改爲HH:mm:ss - >我有同樣的情況。如果我將它改爲HH:mm:s,那麼比用戶鍵入13:14:5的時間長,並且離開輸入區的秒數將保持爲5.在這種情況下,我希望第二個變成05 – Tomtom