我有一个文本框,其文本与字符串数据类型属性绑定。但我只需要在此文本框中输入数值。
它看起来很奇怪,但这是要求之一,同一个文本框可以接受字符串和数值。我不能拿两个文本框来处理这个文本框的可见性。
你可以这样做
<TextBox Text="{Binding Path=Amount}">
<i:Interaction.Behaviors>
<Behaviors:TextBoxInputBehavior InputMode="DigitInput" />
</i:Interaction.Behaviors>
</TextBox>
这些天我在这里发布代码:Validate decimal numbers in a WPF TextBox
您可以使用以下方法很好地做到这一点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
数字,并且不允许输入任何非数字数字。
如果您只需要数值,请执行以下操作。
绑定使用 getter 和 setter。一旦绑定更新了属性的 setter,它就会调用它的 getter。当您在 TextBox 和 setter 中输入文本时,您不会设置值。文本不会被输入。
替代方法是使用 ValidationRules。