Validation for empty text box

user1240679 picture user1240679 · Aug 1, 2012 · Viewed 8.9k times · Source

I have three text boxes on my wpf application. When the user enters the value in these text boxes and clicks on Add, these are added to the list and displayed in data grid.

The text boxes are not bound to anything as such and I add these text box values to an observale collection bound to data grid. I want to prevent the user to enter empty values in text boxes. How is that done?

I saw some examples but those all had the text box boudn to values and then used Binding.Validation. How will this be done in my case when there's binding to text box?

I also have a button which has to be freezed when the empty values are being entered. for that, I sed the following approach by making a class and binding the class in the following manner;

<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; }
    }

Answer

LPL picture LPL · Aug 1, 2012

If I understand you right you're looking for something like this:

<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>

Add this namespace

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

Update

Then this will work:

<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>

And this Converter code

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();
    }
}