Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display all validation error messages in a Common textblock

i am new to WPF, I am trying to Validate some WPF controls i have been following this link WPF Binding Validations

and i have understood how they are doing it. but what i want is to have a common textblock showing error messages for all the controls, in the above link they use a separate textblock for each of the controls to view the error message, but i want to just change the border color of the control to red in case of the error and show the error messages in a textblock which is created to view the error messages for any of the textboxes that has the false input. for example i have a textblock as

<TextBlock Name="txtError" Foreground="Red" Grid.ColumnSpan="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

so whenever the text of any textboxes changes and validation rule returns false i want the border of that textbox red and the message to be displayed in the above textblock.

like image 420
Abdullah Malikyar Avatar asked Oct 20 '22 14:10

Abdullah Malikyar


2 Answers

Welcome to world of bindings, you can solve this issue by just making some more text boxes with bindings (as suggested in prev. answer) and have a style to show problem text box as red.

But I would suggest you to understand the concept of DataValidation and DataErrorInfo in wpf, on a quick search I think This is a good starting point, read about it its worth your time.

After you understand data error info, you can set data error template on your textbox to work around the red border requirement.

like image 77
Muds Avatar answered Oct 22 '22 04:10

Muds


It is much easier if you implement either the IDataErrorInfo Interface or the INotifyDataErrorInfo Interface in your data model class. The first interface provides a string Error property and the second a GetErrors method that returns a collection of errors. You can then data bind these properties to either a TextBlock or an ItemsControl to display all errors that relate to an object.

Not wanting to duplicate the many online examples, I prefer to direct you to them instead. You can find help with implementing these interfaces in the How to: Implement Validation Logic on Custom Objects and How to implement INotifyDataErrorInfo in WPF 4.5? posts online.

Note that although the IDataErrorInfo interface was designed to deal with individual errors, you can alter it to deal with multiple errors if you add an Errors collection property. Take this example:

public string Error
{
    get
    {
        StringBuilder errors = new StringBuilder();
        foreach (string error in Errors) errors.AppendUniqueOnNewLineIfNotEmpty(error);
        return errors.ToString();
    }
}

public override ObservableCollection<string> Errors
{
    get
    {
        errors = new ObservableCollection<string>();
        errors.AddUniqueIfNotEmpty(this["Property1"]);
        errors.AddUniqueIfNotEmpty(this["Property2"]);
        errors.AddUniqueIfNotEmpty(this["PropertyN"]);
        errors.AddRange(ExternalErrors);
        return errors;
    }
}

The AddUniqueIfNotEmpty method is just an extension helper method that I created which 'does what it says on the tin'.

like image 34
Sheridan Avatar answered Oct 22 '22 05:10

Sheridan