0

I made a rule validation on a property. What I want is that when the condition is not satisfied the save button should be disabled. How can I do it? This is how it looks: image. The button is not disabled.

This is my code

public string FieldName { get; set; }
public Regex regularExpression = new Regex("^[a-zA-Z]*$");

public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{

    //var stringValue = value as string;
    if (FieldName == "Age")
        return AgeValidate(value);
    if (FieldName == "Name")
        return NameValidation(value);
    return new ValidationResult(true, null);

}


private ValidationResult NameValidation(object value)
{
    var onlyCharacters = regularExpression.IsMatch((string)value);
    if (onlyCharacters)
        return new ValidationResult(true, null);
    else
        return new ValidationResult(false, $"Only alfabetical charaters");

}

In XAML:

<Label Grid.Row="0" Grid.Column="0" Margin="103,0,98,10" Content="Name : " VerticalContentAlignment="Center" HorizontalContentAlignment="Right" Grid.ColumnSpan="2"/>
<TextBox   Grid.Row="0" Grid.Column="1" x:Name="txtCourtName"  Margin="113,4,-55,6" VerticalContentAlignment="Center" HorizontalContentAlignment="Left"  Validation.ErrorTemplate="{StaticResource errorTemplate}"  >
    <TextBox.Text>
        <Binding Path="CourtName" ValidatesOnDataErrors="true" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <local11:AuthValidation FieldName="Name"></local11:AuthValidation>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

Button:

<Button Content="Save" x:Name="btnSaveCourt"  Command="{Binding SaveCourt}" Margin="2,10" Width="119"  Background="#909090" Foreground="Black" BorderBrush="White" />

And my template for error:

<ControlTemplate x:Key="errorTemplate">
    <Border BorderBrush="OrangeRed" BorderThickness="2">
        <Grid>
            <AdornedElementPlaceholder>
                <TextBlock Text="{Binding [0].ErrorContent}" Foreground="OrangeRed" 
                                   VerticalAlignment="Center" HorizontalAlignment="Right"
                                   Margin="-220"
                                   >
                </TextBlock>
            </AdornedElementPlaceholder>
        </Grid>
    </Border>
</ControlTemplate>
2
  • Instead of using a ValidationRule, you should implement the SaveCourt command's CanExecute method to return false whenever you want to disable the Button. Commented Sep 23, 2019 at 14:11
  • Maybe helpful: stackoverflow.com/a/16340745/11219312 Commented Sep 23, 2019 at 14:46

1 Answer 1

-1

You could bind the button's IsEnabled to a property, a bool propfull (write propfull tab tab and it will write it out for you in visual studio).

Or you could use a MultiDataTrigger on the button and bind all the HasError property of the textboxes you want to validate.

Or if you use a command you could use the can execute property of it.

Sign up to request clarification or add additional context in comments.

3 Comments

Never try to set Button.IsEnabled in MVVM - much better to use an ICommand implementation with a CanExecute mechanism.
It's usually better to go with canexecute but not always ideal. Occasionally you may have logic that doesn't involve user input or lots of commands. In which case you can find invalidaterequerysuggested problematic.
@Peregrine It can be used in xaml in an action too. and its not setting the IsEnabled in MVVM it's binding to it from xaml in case you have some logic in the code too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.