Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 557553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:02:18+00:00 2026-05-13T12:02:18+00:00

I am trying to find an elegant way to validate two related TextBox values

  • 0

I am trying to find an elegant way to validate two related TextBox values in WPF.

The Text property of each text box is bound to a public decimal property on my class which implements INotifyPropertyChanged for TwoWay binding.

I want to validate the two values such that: BiggerValue >= SmallerValue >= 0

I have succeeded in getting each value to validate independently against these conditions using IDataErrorInfo and a string indexer.

My problem is as follows: the user intends to reduce both values and starts with BiggerValue, so that now it is less than SmallerValue. The validation on the BiggerValue TextBox fails (although the value is stored). The user then moves on to the SmallerValue and sets it less than the new BiggerValue. Now both values are valid again, but how can I get the BiggerValue text box to automatically reflect that its (unchanged) value is now valid?

Should I be looking at an event handler like LostFocus() on the text boxes, or adding something like this to the property setters to force a refresh?

biggerValueTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
smallerValueTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();

My full code is below. Somehow it all feels rather clunky and overcomplicated for this simple problem. As a WPF newbie (this is day 2), any comments about my approach, however radical, would be gratefully received.

XAML:

<TextBox x:Name="biggerValueTextBox"
         Text="{Binding Path=BiggerValue,
                        Mode=TwoWay,
                        ValidatesOnDataErrors=True,
                        ValidatesOnExceptions=True}" />
<TextBox x:Name="smallerValueTextBox"
         Text="{Binding Path=SmallerValue,
                        Mode=TwoWay,
                        ValidatesOnDataErrors=True,
                        ValidatesOnExceptions=True}" />

C#:

public partial class MyClass : UserControl,
    INotifyPropertyChanged, IDataErrorInfo
{
    // properties
    private decimal biggerValue = 100;
    public decimal BiggerValue
    {
        get
        {
            return biggerValue;
        }
        set
        {
            biggerValue = value;
            OnPropertyChanged("BiggerValue");
        }
    }
    private decimal smallerValue = 80;
    public decimal SmallerValue
    {
        get
        {
            return smallerValue;
        }
        set
        {
            smallerValue = value;
            OnPropertyChanged("SmallerValue");
        }
    }

    // error handling
    public string this[string propertyName]
    {
        get
        {
            if (propertyName == "BiggerValue")
            {
                if (BiggerValue < SmallerValue)
                    return "BiggerValue is less than SmallerValue.";
                if (BiggerValue < 0)
                    return "BiggerValue is less than zero.";
            }
            if (propertyName == "SmallerValue")
            {
                if (BiggerValue < SmallerValue)
                    return "BiggerValue is less than SmallerValue.";
                if (SmallerValue < 0)
                    return "SmallerValue is less than zero.";
            }
            return null;
        }
    }
    // WPF doesn't use this property.
    public string Error { get { return null; } }

    // event handler for data binding
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:02:18+00:00Added an answer on May 13, 2026 at 12:02 pm

    well, an easy hackish method would be to fire property changed for the biggerValue as well (this will cause a refresh of the validation for the bigger value):

     public decimal SmallerValue
        {
            get
            {
                return smallerValue;
            }
            set
            {
                bool fireForBigger = smallerValue > biggerValue && smallerValue < value;
                smallerValue = value;
                OnPropertyChanged("SmallerValue");
                if (fireForBigger)
                {
                    OnPropertyChanged("BiggerValue");
                }
            }
        }
    

    But, a more solid solution would be to create custom validation rules and set it all up on your own:
    http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validationrules.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find an elegant way to get controls on a Windows
I'm fairly new to LINQ and trying to find a more elegant way (other
I'm trying to find an elegant way to parse a string like: EVENT_TYPE(param1;param2; ...)
I'm trying to find an elegant way to inform a user that s/he is
I'm trying to find the most reusable, yet elegant, piece of code possible for
Been trying to find a working implementation of a WPF listview (or listbox) where
I'm trying to find a way to list the (static) dependency requirements of a
I am trying to find an elegant framework to arrange all of my server
I've looked around, and I'm trying to find an elegant solution to this, and
I am trying to find a generic way of accessing a set of containers.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.