我有一个文本框,其Text与字符串数据类型属性绑定。 但我需要在此文本框中仅输入数值。
它看起来很奇怪,但这是一个要求,同一个文本框可以接受字符串和数值。我不能拿两个文本框并使用文本框的可见性来处理它。
答案 0 :(得分:1)
如果您只需要数值,请执行以下操作。
绑定使用getter和setter。一旦绑定更新属性的setter,它就会调用它的getter。当您在TextBox和setter中输入文本时,您不设置该值。文字不会输入。
替代方案是使用ValidationRules。
答案 1 :(得分:1)
使用AttachedProperty
:
我有一个名为TextBoxProperties
的类,其中包含TextBox
控件的附加属性:
#region IsNumericOnly
/// <summary>
/// Provides the ability to restrict the text input of the TextBox control to only allow numeric values to be entered.
/// </summary>
public static readonly DependencyProperty IsNumericOnlyProperty = DependencyProperty.RegisterAttached("IsNumericOnly", typeof(bool), typeof(TextBoxProperties), new UIPropertyMetadata(default(bool), OnIsNumericOnlyChanged));
/// <summary>
/// Gets the value of the IsNumericOnly property.
/// </summary>
/// <param name="dependencyObject">The DependencyObject to return the IsNumericOnly property value from.</param>
/// <returns>The value of the IsNumericOnly property.</returns>
public static bool GetIsNumericOnly(DependencyObject dependencyObject)
{
return (bool)dependencyObject.GetValue(IsNumericOnlyProperty);
}
/// <summary>
/// Sets the value of the IsNumericOnly property.
/// </summary>
/// <param name="dependencyObject">The DependencyObject to set the IsNumericOnly property value of.</param>
/// <param name="value">The value to be assigned to the IsNumericOnly property.</param>
public static void SetIsNumericOnly(DependencyObject dependencyObject, bool value)
{
dependencyObject.SetValue(IsNumericOnlyProperty, value);
}
/// <summary>
/// Adds key listening event handlers to the TextBox object to prevent non numeric key strokes from being accepted if the IsNumericOnly property value is true, or removes them otherwise.
/// </summary>
/// <param name="dependencyObject">The TextBox object.</param>
/// <param name="dependencyPropertyChangedEventArgs">The DependencyPropertyChangedEventArgs object containing event specific information.</param>
public static void OnIsNumericOnlyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
TextBox textBox = dependencyObject as TextBox;
if (textBox != null)
{
bool newIsNumericOnlyValue = (bool)dependencyPropertyChangedEventArgs.NewValue;
TextCompositionEventHandler textBox_PreviewTextInput = new TextCompositionEventHandler((s, e) => e.Handled = !e.Text.All(c => Char.IsNumber(c) && c != ' '));
KeyEventHandler textBox_PreviewKeyDown = new KeyEventHandler((s, e) => e.Handled = e.Key == Key.Space);
if (newIsNumericOnlyValue)
{
textBox.PreviewTextInput += textBox_PreviewTextInput;
textBox.PreviewKeyDown += textBox_PreviewKeyDown;
}
else
{
textBox.PreviewTextInput -= textBox_PreviewTextInput;
textBox.PreviewKeyDown -= textBox_PreviewKeyDown;
}
}
}
#endregion
它的用法如下:
添加XML命名空间:
xmlns:Attached="clr-namespace:Fully.Qualified.Namespace"
然后是XAML:
<TextBox Text="{Binding YourString}" Attached:TextBoxProperties.IsNumericOnly="True" />
您可以将其应用于任何Textbox
,并且不允许输入任何非数字数字。
答案 2 :(得分:1)
你可以这样做
<TextBox Text="{Binding Path=Amount}">
<i:Interaction.Behaviors>
<Behaviors:TextBoxInputBehavior InputMode="DigitInput" />
</i:Interaction.Behaviors>
</TextBox>
我现在在这里发布代码:Validate decimal numbers in a WPF TextBox