Tom
Tom

Reputation: 33

"IDataErrorInfo" I want to check an error, if it's not "NULL" then it must be numeric

I have a textBlock with a decimal value. Where NULL is allowed. For this i'm using the IDataErrorInfo interface.

If I use the code below and I type in a letter, I don't have a error.

 public override string this[string columnName]
    {
        get
        {

            if (columnName == "Vezels" && !decimal.TryParse(Vezels.ToString(), out decimal decVezels) && Vezels != null)
            {
                return "Vezels must be numeric!";
            }
            return "";
        }
    }

if I leave out != null, he gives an error even if nothing is entered, what can I do about it? I only want an error if a letter is entered. If nothing is entered, the program should just work.

Thank you in advance,

So i use mvvm. I'm bind the property to the xmal.

            <!-- VEZELS -->
            <TextBlock Text="Vezels per gram" FontSize="12"  FontStyle="Italic" Margin="10 10 0 0"/>
            <TextBox Margin="10,0,0,10"
                Text="{Binding Vezels}"
                materialDesign:HintAssist.Hint="Optioneel"
                materialDesign:TransitionAssist.DisableTransitions="True" Width="150" HorizontalAlignment="Left" FontSize="12" FontStyle="Italic" />

This is the code in my viewModel

    private decimal? _vezels;


    public decimal? Vezels
    {
        get { return _vezels; }
        set { _vezels = value;
            NotifyPropertyChanged();
        }
    }


// IDataErrorInfo
    public abstract string this[string columnName] { get; }

    public bool IsGeldig()
    {
        return string.IsNullOrWhiteSpace(Error);
    }

    public string Error
    {
        get
        {
            string foutmeldingen = "";
            foreach (var item in this.GetType().GetProperties())
            {
                string fout = this[item.Name];
                if (!string.IsNullOrWhiteSpace(fout))
                {
                    foutmeldingen += fout + Environment.NewLine;
                }
            }
            return foutmeldingen;
        }
    }

code in viewmodel

 public void Opslaan()
    {
        if (IsGeldig())
        {
            Artikel artikel = new Artikel();
            artikel.Eiwitten = this.Eiwitten;
            artikel.Beschrijving = this.Beschrijving;
            artikel.Koolhydraten = this.Koolhydraten;
            artikel.Vetten = this.Vetten;
            artikel.Vezels = this.Vezels;

            int ok = DataBaseOperations.ToevoegenArtikel(artikel);
            if (ok <= 0)
            {
                MessageBox.Show("Artikel is niet toegevoegd");
            }
            else
            {
                VoedselZoekenViewModel voedselZoekenViewModel = new VoedselZoekenViewModel();
                VoedselZoekenView voedselZoekenView = new VoedselZoekenView();
                voedselZoekenView.DataContext = voedselZoekenViewModel;
                LoggedInUser.LastView = voedselZoekenView;
                voedselZoekenView.ShowDialog();
            }
        }
        else
        {
            MessageBox.Show(Error);
        }

    }

Upvotes: 0

Views: 94

Answers (0)

Related Questions