user3605194
user3605194

Reputation: 51

Why is my IDdataErrorInfo validation implementation is not being picked up?

I've written an IDataErrorInfo class that provides some input validation. The problem I'm having is that it's not being detected at all, no validation is showing. It used to work a few months ago. I haven't changed any of the code relating to this validation class.

I've tried switching between Release/Debug, x86/x64, rebuilding/cleaning, deleting the shadow cache. Nothing has helped, unfortunately.

This is how I'm referencing my validation class in xaml:

xmlns:local="clr-namespace:Pharmatech"

The validation itself in xaml:

<Style TargetType="TextBox">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path= (Validation.Errors).CurrentItem.ErrorContent}"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

Calling it on the textboxes:

Text="{Binding Id, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"

My validation class is definitely under the namespace 'Pharmatech':

namespace Pharmatech
{
public class PatientValidation : INotifyPropertyChanged, IDataErrorInfo
{
    private string _id;

public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string p)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(p));

    }

    public string Id
    {
        get
        {
            return _id;
        }

        set
        {
            _id = value;
        }
    }
public string this[string PropertyName]
    {
        get
        {
            string result = null;

            switch (PropertyName)
            {
                case nameof(Id):
                    if (string.IsNullOrEmpty(Id))
                        result = "ID number is required.";

                    else if (InputValidation.validateIDNumber(Id) != true)
                        result = "Invalid ID number.";
                    break;
            }
            return result;
}
}

Any help would be GREATLY appreciated - I have a feeling I'm missing something obvious.... or, VS is messing with me.

I guess it's worth noting that I've added the UI styling library Metro or MahApps recently. I've tried removing it, but to no avail.

Upvotes: 0

Views: 51

Answers (1)

user3605194
user3605194

Reputation: 51

Thank you for everyone trying to help out here.

Although this may not be the best way do it following WPF and MVVM - I fixed it by setting the DataContext as follows:

<Window.DataContext>
    <local:PatientValidation></local:PatientValidation>
</Window.DataContext>

Upvotes: 1

Related Questions