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>
Palle Due
  • 5,929
  • 4
  • 17
  • 32
robert.laszlo
  • 21
  • 1
  • 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`. – mm8 Sep 23 '19 at 14:11
  • Maybe helpful: https://stackoverflow.com/a/16340745/11219312 – AmRo Sep 23 '19 at 14:46

1 Answers1

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

Edwinke
  • 69
  • 1
  • 6
  • 2
    Never try to set Button.IsEnabled in MVVM - much better to use an ICommand implementation with a CanExecute mechanism. – Peregrine Sep 23 '19 at 14:24
  • 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. – Andy Sep 23 '19 at 17:37
  • @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. – Edwinke Sep 24 '19 at 08:41