Validation using IDataErrorInfo

389 views Asked by At

I'm using IDataErrorInfo to implement some basic logic validation for some values. This seems to work well, and I'm using a contentpresenter to display the results:

<ContentPresenter Content="{ Binding ElementName =MyElement, Path=(Validation.Errors).CurrentItem}"
                              HorizontalAlignment ="Left">
     <ContentPresenter.ContentTemplate>
           <DataTemplate>
                <TextBlock Foreground ="Red" FontStyle="Italic" Text="{ Binding Path =ErrorContent}" />
           </DataTemplate>
     </ContentPresenter.ContentTemplate>
</ContentPresenter>

I get a nice red message when there's a problem, and the field in question is correctly highlighted. However, when this happens, I'd like to disable the save button for the form. Here's what I've tried so far (without success):

<Button Content="Save" Click ="MyButton_Click"
            IsEnabled="{Binding Converter={StaticResource ValidConverter}, ConverterParameter={Binding ElementName=MyElement, Path=(Validation.Errors).CurrentItem}}"/>

ValidConverter is just a converter that returns true for a null or empty string.

I also tried triggers, like this (tried both Trigger and DataTrigger):

<Button Content="Save" Click ="MyButton_Click"
    <Button.Style>
        <Style>                        
            <Style.Triggers>
                <Setter Property ="Button.IsEnabled" Value="True"   />
                <DataTrigger Binding ="{ Binding Path=(Validation.HasError)}" Value ="True">
                     <Setter Property ="Button.IsEnabled" Value="False"   />
                </ DataTrigger>
             </Style.Triggers>
        </Style>
    </ Button.Style>
</ Button>

I've found some on-line information on this, and as far as I can tell the trigger method should work; however, if I use Trigger, nothing happens, and DataTrigger doesn't compile (error MC1000: Unknown build error, 'Index (zero based) must be greater than or equal to zero and less than the size of the argument list).

Can anyone tell me why this doesn't work, and what I'm doing wrong here?

2

There are 2 answers

0
Paul Michaels On BEST ANSWER

The problem was with the default:

<Style.Triggers>                
    <DataTrigger Binding ="{ Binding Path=(Validation.HasError)}" Value ="True">
        <Setter Property ="Button.IsEnabled" Value="False" />
    </ DataTrigger>
</Style.Triggers>

Works fine. As @Shoe pointed out - there was a typo in the question - now corrected.

0
Noctis On

Hmm ... I'm doing it the other way around ... here's a similar button, but it's based on several validations for several TextBoxs and ComboBox that need to be validated.

So, I'll set it to false by default, and when all the validations errors are false, set it to true.

<Button x:Name="Btn_Insert"
        Command="{Binding Insert_Command}"
        IsDefault="True"
        >
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="IsEnabled" Value="False"/>
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Path=(Validation.HasError),
                                   ElementName=txtbx_1}" Value="False"/>
                        <Condition Binding="{Binding Path=(Validation.HasError),
                                    ElementName=cmb_1}" Value="False"/>
                        <Condition Binding="{Binding Path=(Validation.HasError),
                                    ElementName=txtbx_2}" Value="False"/>
                        <Condition Binding="{Binding Path=(Validation.HasError),
                                    ElementName=txtbx_3}" Value="False"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="IsEnabled" Value="True"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

Try also putting your <Setter Property ="Button.IsEnabled" Value="True" /> above the <Style.Triggers>