Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to disable ValidatesOnDataErrors on a TextBox if a certain checkbox is checked. I have tried placing a trigger on textbox to enable or disable validation based on the checkbox seems like the trigger gets hit but does not disable validation. I am using IDataErrorInfo for validation in the .cs code. Here is the code I have tried, this has been a headache so hope you can help.

.xaml

<TextBox Name="txtFoundERTReading" Height="23" Canvas.Left="125" TextWrapping="Wrap" Canvas.Top="136" Width="120">
    <TextBox.Style>                                
        <Style TargetType="{x:Type TextBox}">                                    
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=cbFoundERTReading, Path=IsChecked}" Value="False">
                    <Setter Property="Text" Value="{Binding Found.ERTReading, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=cbFoundERTReading, Path=IsChecked}" Value="True">
                    <Setter Property="TextBox.IsEnabled" Value="False" />
                    <Setter Property="Text" Value="{Binding Found.ERTReading, Mode=TwoWay, ValidatesOnDataErrors=False, UpdateSourceTrigger=PropertyChanged}" />                                            
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
share|improve this question

1 Answer 1

Instead of changing the ValidatesOnDataErrors property at run time, the best approach is to have a boolean property in viewmodel and do validation only if it is true. The boolean property can bound to IsChecked property of a Checkbox.

    public string Name
    {
        get { return name; }
        set { name = value; RaisePropertyChanged("Name"); }
    }

    public string this[string columnName]
    {
        get
        {
            if (CanValidate)
            {
                if (columnName == "Name")
                {
                    if (!ValidateName())
                    {
                        return "Error";
                    }
                }
            }

            return "";
        }
    }

    private bool canValidate;

    public bool CanValidate
    {
        get { return canValidate; }
        set { canValidate = value; RaisePropertyChanged("CanValidate"); RaisePropertyChanged("Name");}
    }

    private bool ValidateName()
    {
        if (String.IsNullOrEmpty(Name))
        {
            return false;
        }
        return true;
    }

The XAML looks like below,

    <StackPanel>
        <TextBox Margin="5" Text="{Binding Name, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}"/>
        <CheckBox Margin="5" Content="Can validate" IsChecked="{Binding CanValidate, Mode=TwoWay}"/>
    </StackPanel>
share|improve this answer
    
Thanks much for a quick reply. I have 5 textboxes and each of them have a corresponding checkbox. I would hate to have 5 bool properties to check for each textbox? Is there a way to avoid that? The 5 textboxes are required unless you click on a checkbox that says "Unable to Read" that is when I would like to disable validation and remove the red color from textbox border (Caused by ValidatesOnDataError). –  Nick Manojlovic May 29 '13 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.