c# - 输入验证 Silverlight

标签 c# silverlight xaml

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

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

文本框只接受数字( double )并在输入其他内容(字母或符号)时立即显示警告。

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

有什么想法吗?

最佳答案

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

我的代码是针对服务器上的一个端口的,所以只有数字,但添加 .对于 double (我认为“[^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; 
}

关于c# - 输入验证 Silverlight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7596865/

相关文章:

c# - 使用 C# 获取目录中带有 *.tif 文件掩码的文件

c# - 程序崩溃后如何挂起所有线程?

javascript - 如何在asp.net中单击按钮下载html文件?

c# - NHibernate (Hibernate) 属性自动递增

silverlight - Silverlight Media Framework 中 Windows Phone 7 的全屏控制

javascript - 强制 SSRS 报告中的链接从 silverlight 中在新窗口中打开

c# - RibbonMenuButton 绑定(bind)项目单击以执行命令

silverlight - 有没有办法对从 ItemsControl 的 ItemTemplate 生成的 RadioButtons 进行分组

c# - 当我在同一空间中有 2 个可鼠标悬停对象时,如何在 Siverlight 中处理鼠标悬停事件?

c# - ResourceDictionary 键的顺序是如何确定的?