验证空文本框

时间:2012-08-01 14:39:35

标签: c# wpf validation multidatatrigger

我的wpf应用程序上有三个文本框。当用户在这些文本框中输入值并单击Add时,这些值将添加到列表中并显示在数据网格中。

文本框不会绑定到任何内容,我将这些文本框值添加到绑定到数据网格的observale集合中。我想阻止用户在文本框中输入空值。怎么做的?

我看到了一些示例,但是那些都有文本框boudn到值,然后使用Binding.Validation。在绑定到文本框时,如何在我的情况下完成此操作?

我还有一个按钮,在输入空值时必须冻结。为此,我通过创建一个类并以下面的方式绑定类来进行以下方法;

<Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="IsEnabled" Value="false" />
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=textBox1, Path=(Validation.HasError)}" Value="false" />
                                <Condition Binding="{Binding ElementName=textBox2, Path=(Validation.HasError)}" Value="false" />
                                <Condition Binding="{Binding ElementName=TextBoxAge, Path=(Validation.HasError)}" Value="false" />                                
                            </MultiDataTrigger.Conditions>
                            <Setter Property="IsEnabled" Value="true" />
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>

.cs class

 public class TextBoxNotEmptyValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            string str = value as string;
            if (str != null)
            {
                if (str.Length > 0)
                    return ValidationResult.ValidResult;
            }
            return new ValidationResult(false, Message);
        }
        public string Message { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

如果我理解你,你正在寻找这样的东西:

<Button.Style>
    <Style TargetType="{x:Type Button}">
        <Setter Property="IsEnabled" Value="True" />
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Text, ElementName=textBox1}" Value="{x:Static s:String.Empty}" />
                    <Condition Binding="{Binding Text, ElementName=textBox2}" Value="{x:Static s:String.Empty}" />
                    <Condition Binding="{Binding Text, ElementName=TextBoxAge}" Value="{x:Static s:String.Empty}" />
                </MultiDataTrigger.Conditions>
                <Setter Property="IsEnabled" Value="False" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</Button.Style>

添加此命名空间

xmlns:s="clr-namespace:System;assembly=mscorlib"

<强>更新

然后这将起作用:

<Button.Style>
    <Style TargetType="{x:Type Button}">
        <Style.Resources>
            <local:MyTextValidationConverter x:Key="MyTextValidationConverter" />
        </Style.Resources>
        <Setter Property="IsEnabled">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource MyTextValidationConverter}">
                    <Binding Path="Text" ElementName="textBox1" />
                    <Binding Path="Text" ElementName="textBox2" />
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</Button.Style>

这个转换器代码

public class MyTextValidationConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string text1 = values[0] as string;
        if (string.IsNullOrEmpty(text1)) return false;

        string text2 = values[1] as string;
        if (string.IsNullOrEmpty(text2)) return false;

        return true;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}