C# 时间文本框
在我的应用程序中,我有一个C# 时间文本框,c#,wpf,C#,Wpf,在我的应用程序中,我有一个文本框,用户可以在其中以HH:mm:ss格式输入时间值 文本框的xaml如下所示: <TextBox xMinWidth="60" HorizontalAlignment="Left" Background="Transparent"> <TextBox.Text> <Binding Path="FileTime" UpdateSourceTrigger="PropertyChanged" StringFormat=
文本框
,用户可以在其中以HH:mm:ss
格式输入时间值
文本框的xaml如下所示:
<TextBox xMinWidth="60" HorizontalAlignment="Left" Background="Transparent">
<TextBox.Text>
<Binding Path="FileTime" UpdateSourceTrigger="PropertyChanged" StringFormat="T" ConverterCulture="de-DE" />
</TextBox.Text>
</TextBox>
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);
}
}
ViewModel中的属性FileTime
的类型为DateTime
如果我尝试键入值13:15:45,则在键入4时,文本框将显示13:15:04。如果我键入最后5个,则文本框中的结果为13:15:045,这不是有效时间
如何使我的TextBox
接受一个数字作为秒,而不附加前导零?我找到了一个非常适合我的解决方案,并且每个TextBox
都可以轻松地重复使用
我已经写了一个附件属性
,它看起来像:
<TextBox xMinWidth="60" HorizontalAlignment="Left" Background="Transparent">
<TextBox.Text>
<Binding Path="FileTime" UpdateSourceTrigger="PropertyChanged" StringFormat="T" ConverterCulture="de-DE" />
</TextBox.Text>
</TextBox>
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>
?为什么不尝试更改StringFormat?@Tomtom为什么会这样HH:mm:s
的意思是:给我两个数字表示小时,如果小时<10,给我两个数字表示分钟,如果分钟<10,给我两个数字表示分钟,如果分钟<10,给我一个数字表示秒,如果秒>10,给我两个数字。你为什么不更新失去焦点的模型?在PropertyChanged
中更新它是否很有必要?如果用户正在输入数据,并且只有在文本框失去焦点后才对文本进行格式化,那么将文本保留为未格式化如何?这也是数字格式的常见解决方案,如