gpt4 book ai didi

c# - 时间文本框

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:31 24 4
gpt4 key购买 nike

在我的应用程序中,我有一个 TextBox,用户可以在其中输入格式为 HH:mm:ss 的时间值。

TextBox 的 xaml 如下所示:

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

ViewModel 中的属性 FileTimeDateTime 类型。

如果我尝试输入值 13:15:45,TextBox 会在我输入 4 时显示 13:15:04。如果我输入最后 5 个,则结果在TextBox 是 13:15:045,这不是有效时间。

如何让我的 TextBox 接受一个数字作为第二个数字并且不附加前导零?

最佳答案

我找到了一个非常适合我的解决方案,并且可以轻松地为每个 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>

关于c# - 时间文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34065124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com