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

  • SEARCH
  • Home
  • 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 9140587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:32:09+00:00 2026-06-17T09:32:09+00:00

I am attempting to implement some simple validation on a textbox in MVVM public

  • 0

I am attempting to implement some simple validation on a textbox in MVVM

    public string Property
    {
        get
        {
            if (App.PropertyStorageContainer != null)
            {
                return App.PropertyStorageContainer.Property;
            }
            else
            {
                return null;
            }
        }
        set
        {
            App.PropertyStorageContainer.Property = value;
            RaisePropertyChanged("Property");
        }
    }

Then in my PropertyStorageContainer class I have

private string _property;
public string Property
    {
        get
        {               
            return App.PropertyStorageContainer.Property;
        }
        set
        {
            if(value meets some condition)
            {
                _property = value;
            }
            else
            {
               _property = someothervalue;
            }
        }
    }

.

   <TextBox Width="50" TextAlignment="Center" Text="{Binding Property, Mode=TwoWay, NotifyOnValidationError=True}" MaxLength="3"></TextBox>                          

The point of this is to validate what goes in the box. Now if I set this value directly from my code then everything works as I would expect. It attempts to SET the value, then calls RaiseProperyChanged, then GET the value (which because of the validation may not be the same value that was entered originally). The final value retrieved does show up on the view, so I know TwoWay binding is working.

The problem I have is when the input for SET comes from the bound XAML property / directy from user. In this case the SET method is called, the validation performed, but the GET never happens. This results in the unvalidated value remaining in the textbox on screen.

My first question would be is this a bug or expected behavior? I can see how maybe they tried to save performance by removing that last GET when the input came straight from the user since there should be nothing new to GET. But if not then maybe the way I have it all setup is interfering with the GET being called.

Second question is of course any suggestions for getting around this one. I’ve read a few suggestions for other methods of doing validation, but my program is already live on PROD and most of the changes being suggested involve a lot of rework for me so I am hoping to find a way to make it call GET any time the property is SET.

  • 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-06-17T09:32:10+00:00Added an answer on June 17, 2026 at 9:32 am

    I have made a couple of assumptions since I am not sure I understand you code completely but I think you could consider possibly implementing a custom validation rule. First off, since your custom ValidationRule will take care of the validation you could get the logic out of your model class’s property definition and “dumb down” your poco:

    class PropertyStorageContainer
    {
        public string Property { get; set; }
    }
    

    It seems you desire your view model to act as a basic wrapper around your model class. Again, I will assume this is valid based on the description of your scenario:

    class PropertyStorageContainerViewModel : INotifyPropertyChanged
    {
        private PropertyStorageContainer model;
    
        public PropertyStorageContainerViewModel(PropertyStorageContainer model)
        {
            this.model = model;
        }
    
        public string Property
        {
            get
            {
                if (model != null)
                {
                    return model.Property;
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (model.Property != value)
                {
                    model.Property = value;
                    RaisePropertyChanged("Property");
                }
            }
        } 
        // INotifyPropertyChanged implementation...
    }
    

    Now create a new class that extends System.Windows.Controls.ValidationRule and override the abstract Validate method in order implement your validation logic. For the example, I created a rule that just checks if the string is null or empty (assuming that would be an invalid scenario):

    class IsNullOrEmptyValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            string s = (value ?? string.Empty).ToString();
            if (string.IsNullOrEmpty(s))
            {
                // Invalid...
                return new ValidationResult(false, "Please enter a value.");
            }
            else
            {
                // Valid...
               return new ValidationResult(true, null);
            }
        }
    }
    

    Now for the XAML… Here is an example of a TextBox that adds the validation rule to its binding validation rules (can be multiple rules).

     <TextBox Name="textBox1" Width="50" FontSize="12"
            Validation.ErrorTemplate="{StaticResource validationTemplate}"
            Style="{StaticResource textBoxInError}">
        <TextBox.Text>
            <Binding Path="Property" UpdateSourceTrigger="PropertyChanged" >
                <Binding.ValidationRules>
                    <local:IsNullOrEmptyValidationRule />
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox> 
    

    Then define the following resources (referenced above) somewhere (e.g., Window.Resources). First a ControlTemplate to define how the TextBox should look when in invalid state:

    <ControlTemplate x:Key="validationTemplate">
        <DockPanel>
            <TextBlock Foreground="Red" FontSize="15" Text="!!!" />
            <AdornedElementPlaceholder/>
        </DockPanel>
    </ControlTemplate>
    

    Additionally you could define a style trigger to display the error message. Here I just bind it to the ToolTip property of the TextBox:

    <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" 
                        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to implement a simple Single Sign On scenario where some of the
I am attempting to implement some Facebook popups for my native FB app but
I am attempting to implement some very basic debugger protection to prevent kids from
I am attempting to implement some additional statistics gathering in a C# server application
I seem to be having some issues while attempting to implement logging into my
I'm attempting to create a simple web application for some personal development, but I've
I'm attempting to implement the latest Facebook Connect SDK and I'm having some troubles.
I'm attempting to implement the technique for data validation from Josh Smith's example here:
I'm attempting to do some validation for user input using Express and Mongoose. Previously,
I am attempting to implement a very simple Trie in Java that supports 3

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.