Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF ValidationRule,用于检查文本框是否仅包含字母_C#_Wpf_Validation_Xaml - Fatal编程技术网

C# WPF ValidationRule,用于检查文本框是否仅包含字母

C# WPF ValidationRule,用于检查文本框是否仅包含字母,c#,wpf,validation,xaml,C#,Wpf,Validation,Xaml,我正在尝试实现验证用户只在文本框中输入字母。我下面的代码与上面链接的示例略有不同,其中属性Min和Max是固定的数字;而在我的例子中,属性alphabets only包含字母范围 <Binding.ValidationRules> <local:AlphabetsOnlyValidationRule AlphabetsOnly="????" /> </Binding.ValidationRules> 问题:在主窗口.xaml文件

我正在尝试实现验证用户只在
文本框中输入
字母。我下面的代码与上面链接的示例略有不同,其中属性Min和Max是固定的数字;而在我的例子中,属性
alphabets only
包含字母范围

<Binding.ValidationRules>
     <local:AlphabetsOnlyValidationRule AlphabetsOnly="????" />
</Binding.ValidationRules>
问题:在
主窗口.xaml
文件(显示在底部)的以下部分中,我应该用什么替换
?我发现了类似的内容,但他们声明了一个属性
MustEndWith
,该属性指示值应以单词
.def
结尾-这与说所有字符都必须是字母不同

<Binding.ValidationRules>
     <local:AlphabetsOnlyValidationRule AlphabetsOnly="????" />
</Binding.ValidationRules>
主窗口.xaml

public class AlphabetsOnlyValidationRule : ValidationRule
{
    public string AlphabetsOnly { get; set; }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        string sVal = value as string;

        if (sVal == null)
        {
            return new ValidationResult(false, "Please enter some text");
        }

        if (!sVal.ToList().All(c=>char.IsLetter(c)))
        {
            return new ValidationResult(false, "First Name should contains alphabets only"));
        }

        return new ValidationResult(true, null);
    }
}
<Window x:Class="WPF_DeleteNow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        .....
        xmlns:local="clr-namespace:MyWPFProject"
        mc:Ignorable="d">

    <Window.Resources>
        <local:AlphabetsOnlyValidationRule x:Key="AlphabetsOnlyKey"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            .....
        </Grid.RowDefinitions>
        <TextBox Name="txtFirstName">
            <TextBox.Text>
                <Binding Path="FirstName" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <local:AlphabetsOnlyValidationRule AlphabetsOnly="??????" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </Grid>
</Window>

.....

您不需要属性来验证字符串是否只包含字母。只需删除
alphabets only
属性并使用正则表达式执行验证:

public class AlphabetsOnlyValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        string sVal = value as string;

        if (string.IsNullOrEmpty(sVal))
        {
            return new ValidationResult(false, "Please enter some text");
        }

        if (System.Text.RegularExpressions.Regex.IsMatch(sVal, @"^[a-zA-Z]+$"))
        {
            return new ValidationResult(false, "First Name should contains alphabets only");
        }

        return new ValidationResult(true, null);
    }
}

在您的示例中,
字母表应该设置为什么?@mm8是的,这是我的困惑。正如您在block
if(!sVal.ToList().All(c=>char.isleter(c)){…}
中注意到的,我正在检查文本值是否只包含
字母表。但是我很确定
字母表应该设置为什么。有什么意见/建议吗?我知道在正则表达式中,我们会执行类似于
[a-zA-Z]
的操作,但我不确定在上面的类中,
alphabets only
将设置为什么。如果您不知道为什么需要该属性或将其设置为什么,为什么不将其删除?