简体   繁体   English

WPF 中 IP 地址的文本框验证

[英]Textbox validation for IP Address in WPF

I'm making a WPF Application using C#.我正在使用 C# 制作 WPF 应用程序。 I want to put validations on integers (0123456789 and ".") only.. The textbox is supposed to contain an IP address... So need to ensure that user key in their correct "IP Address" before they click on the "Submit" button... How can I achieve it?我只想对整数(0123456789 和“.”)进行验证。文本框应该包含一个 IP 地址......所以需要确保用户在点击“提交”之前输入正确的“IP 地址” " 按钮......我怎样才能实现它?

Thanks.谢谢。

You can easily implement this using Wpf binding validation rules or by using a custom masked textbox您可以使用 Wpf 绑定验证规则或使用自定义屏蔽文本框轻松实现此功能

Check these links for exactly what you are looking for检查这些链接以了解您正在寻找的内容

http://geekswithblogs.net/QuandaryPhase/archive/2008/12/17/wpf-masked-textbox.aspx http://geekswithblogs.net/QuandaryPhase/archive/2008/12/17/wpf-masked-textbox.aspx

http://www.switchonthecode.com/tutorials/wpf-tutorial-binding-validation-rules http://www.switchonthecode.com/tutorials/wpf-tutorial-binding-validation-rules

Hope it helps..希望它有帮助..

The following question link on StackOverflow contains a lot of pointers to MaskedTextBox implementation in WPF . StackOverflow 上的以下问题链接包含许多指向WPF MaskedTextBox实现的指针。 You can use it to get IP Address input from user.您可以使用它来获取用户输入的IP Address

Where can I find a free masked TextBox in WPF? 我在哪里可以找到 WPF 中的免费蒙版 TextBox?

Sounds like you're trying to implement a masked textbox, which is a textbox that auto-format data as the user types according to a specified pattern.听起来您正在尝试实现一个掩码文本框,这是一个文本框,它可以根据指定的模式在用户键入时自动格式化数据。 Check this tutorial on how to implement this, since is not featured in WPF out-of-the-box: Masked TextBox in WPF查看本教程,了解如何实现这一点,因为 WPF 中没有开箱即用的功能: WPF中的Masked TextBox

 <TextBox IsReadOnly="False" Name="txtIpAddress">
                            <TextBox.Text>
                                <Binding Path="IpAddress" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True" >
                                    <Binding.ValidationRules>
                                        <Local:IPValidationRule />
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>
Public class IPValidationRule: ValidationRule
    {
     public override ValidationResult Validate(object value,System.Globalization.CultureInfo cultureInfo)
    {
    if(value == Rejex.Match(your condtion)
    {
    return new ValidationResult(true, null);
    }
    else
    {               
     return new ValidationResult(false, "Invalid_Address");
    }
    }
    }
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM