输入验证 Silverlight

发布于 2024-12-06 21:49:45 字数 277 浏览 0 评论 0原文

我有一个未绑定的文本框。

<TextBox x:Name="inputBox" Grid.Column="1" Grid.Row="1"  />

文本框仅接受数字(双精度),并在将其他内容(字母或符号)写入框中时显示警告。

在 TextChanged 事件中,我根据输入的值进行一些计算并将其显示在 TextBlock 中,因此我需要某种方法来验证输入是用户在框中写入的数字,但我很难找到一个好方法来做到这一点。

有什么想法吗?

i have a textbox which is not bound.

<TextBox x:Name="inputBox" Grid.Column="1" Grid.Row="1"  />

The textbox is to only accept numbers (doubles) and show a warning at once something else(letters or symbols) is written in to the box.

On the TextChanged event, i do some calculations depending on the value enterd and show it in a TextBlock, therefor i need some way of validating that the input is a number as the user writes in the box, but i am having a hard time finding a good way to do this.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度° 2024-12-13 21:49:45

我之前使用的是正则表达式来禁止非数字字符。也许这是可以适应的东西?

我的代码是针对服务器上的端口的,因此只有数字,但应该很容易添加 .对于双打(我认为“[^0-9\.]”应该有效,但正则表达式不是我非常擅长的东西:-))

// Text change in Port text box
private void txtPort_TextChanged(object sender, TextChangedEventArgs e)
{
    // Only allow numeric input into the Port setting.
    Regex rxAllowed = new Regex(@"[^0-9]", RegexOptions.IgnoreCase);

    txtPort.Text = rxAllowed.Replace(txtPort.Text, ""); 
    txtPort.SelectionStart = txtPort.Text.Length; 
}

What I have used before is a regex to disallow non-numeric characters. Maybe this is something that could be adapted?

My code was for a port on a server so only numbers but should be straightforward to add the . for doubles (I think "[^0-9\.]" should work but regexs aren't something I am fantastically good at :-) )

// Text change in Port text box
private void txtPort_TextChanged(object sender, TextChangedEventArgs e)
{
    // Only allow numeric input into the Port setting.
    Regex rxAllowed = new Regex(@"[^0-9]", RegexOptions.IgnoreCase);

    txtPort.Text = rxAllowed.Replace(txtPort.Text, ""); 
    txtPort.SelectionStart = txtPort.Text.Length; 
}
分开我的手 2024-12-13 21:49:45

这是何时使用行为的另一个示例。

public class TextBoxValidator : Behavior<TextBox>
{
  protected override void OnAttached()
  {
    AssociatedObject.TextChanged += new TextChanged(OnTextChanged);
  }

  private void OnTextChanged(object sender, TextChangedEventArgs e)
  {
    // Here you could add the code shown above by Firedragon or you could
    // just use int.TryParse to see if the number if valid.
    // You could also expose a Regex property on the behavior to allow lots of
    // types of validation
  }
}

您没有真正解释当用户输入无效值时您想要采取什么操作。

This is another example of when to use a Behavior.

public class TextBoxValidator : Behavior<TextBox>
{
  protected override void OnAttached()
  {
    AssociatedObject.TextChanged += new TextChanged(OnTextChanged);
  }

  private void OnTextChanged(object sender, TextChangedEventArgs e)
  {
    // Here you could add the code shown above by Firedragon or you could
    // just use int.TryParse to see if the number if valid.
    // You could also expose a Regex property on the behavior to allow lots of
    // types of validation
  }
}

You didn't really explain what actions you wanted to take when the user enters an invalid value.

爱给你人给你 2024-12-13 21:49:45

也许最好使用 BindingValueConverter 来更新 TextBlock 的内容。在这种情况下,您可以在转换器内实现数值验证。

Maybe it would be better to use Binding with ValueConverter that will update TextBlock's content. In this case you can implement validation for numeric value in within converter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文