Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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# 如何在不使用绑定路径的情况下对必填字段进行验证_C#_Wpf_Xaml - Fatal编程技术网

C# 如何在不使用绑定路径的情况下对必填字段进行验证

C# 如何在不使用绑定路径的情况下对必填字段进行验证,c#,wpf,xaml,C#,Wpf,Xaml,我现在的问题是, 对于我当前拥有的每个文本框,我必须指定一个绑定路径,以验证文本框是否为空。 然而,如果碰巧我有100个文本框,我就不可能为所有100个文本框单独创建一个get和set方法。那么,有没有更好的方法来进行我现在拥有的当前验证 以下是我目前拥有的代码 在XAML中 <Grid.BindingGroup> <BindingGroup Name="RequiredFields"> <BindingGr

我现在的问题是, 对于我当前拥有的每个文本框,我必须指定一个绑定路径,以验证文本框是否为空。 然而,如果碰巧我有100个文本框,我就不可能为所有100个文本框单独创建一个get和set方法。那么,有没有更好的方法来进行我现在拥有的当前验证

以下是我目前拥有的代码

在XAML中

 <Grid.BindingGroup>
            <BindingGroup Name="RequiredFields">
                <BindingGroup.ValidationRules>
                    <local:MandatoryFieldRule ValidationStep ="CommittedValue"/>
                </BindingGroup.ValidationRules>
            </BindingGroup>
        </Grid.BindingGroup>
            <TextBox x:Name="ds_instruct" HorizontalAlignment="Left" Height="30"
               Margin="286,186,0,0" TextWrapping="Wrap" VerticalAlignment="Top"    
               Width="275" FontSize="11" GotFocus="textBox_Expand"                                                     
               LostFocus="textBox_Expand" Tag="Default Special Instruction" 
               SpellCheck.IsEnabled="True" 
               Text="{Binding  Path=Text, BindingGroupName=RequiredFields,ValidatesOnDataErrors=true}"/>
所以我的问题是,如何验证多个文本框的必填字段,而不将一对一bindingTextBox指定给getter和setter方法

提前谢谢大家

也许您可以使用反射来检查字符串的值

public string this[string columnName]
{
    get 
    {
        string result = null;
        if (string.IsNullOrEmpty(this.GetType().GetProperty(columnName).GetValue(this) as string))
        {
            result = "Mandatory field required";
        }
        return result;
    }
}

您是如何在运行时创建TextBoxs 100 nos的?或者上面的xaml是您重用的用户控件?嗨,Jacob,在某种程度上,我的意思是,对于绑定路径,是否可以在不指定get和set方法的情况下从文本中获取值?就绑定而言,UI TextBox中的每个元素都应该绑定到一个属性,可以是相同的属性,也可以是不同的属性。是否可以遍历已加载的控件?但在此,您可能需要再次为每个控件指定验证。您可以尝试此绑定验证:。这将帮助您编写自定义验证器。在运行时,每当您创建一个新的TextBox钩子/绑定它的验证器到您创建的这个自定义验证器。嗨,jacob,这是一个有趣的想法,但我放弃了,并为每个元素指定了一个属性,创建了一个实体模型来存储它。这是因为我必须对每个用户控件字段进行数据验证。不过谢谢你的建议,总有一天我会试试的
public string this[string columnName]
{
    get 
    {
        string result = null;
        if (string.IsNullOrEmpty(this.GetType().GetProperty(columnName).GetValue(this) as string))
        {
            result = "Mandatory field required";
        }
        return result;
    }
}