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 7997585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:59:52+00:00 2026-06-04T14:59:52+00:00

I’m trying to implement elegant form validation in a C# / WPF / Entity

  • 0

I’m trying to implement elegant form validation in a C# / WPF / Entity Framework 4.0 application. I have a classic form (some textboxes and checkboxes) and a save button.
I only want to update the source (and database) when the user presses save, and I also only want to run the form validation when the user presses save.

I set all my bindings with the parameter UpdateSourceTrigger=Explicit. I also placed all the bindings within a BindingGroup.

When the user presses save I trigger the UpdateSources method of the binding group – this triggers UpdateSource on each binding. At this point (before the source update completes) I want the form validation to take place and errors to be highlighted in the GUI. If there are no input errors then the update should be free to go through.

I thought I could achieve this by implementing IDataErrorInfo on the EntityObject the fields are bound to, and setting the parameter ValidatesOnDataErrors=True on all my bindings.

Unfortunately this does not work because as explained here:
MSDN Databinding Overview – Data Validation
under the heading “Validation Process”

5) The binding engine sets the source property.

6) …… This is the point when bindings that have the ValidatesOnDataErrors set to true are checked.

This seems really stupid to me – why would you want to validate the data after it has already been “committed” to the object? I’ve been searching for hours for a way to get the behavior I want… Has someone done something like this before?

So the main question is:
How can I validate the input BEFORE the source is updated and cancel the update if the validation fails?

  • 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-04T14:59:53+00:00Added an answer on June 4, 2026 at 2:59 pm

    Value has to be commited to the object because IDataErrorInfo uses only propertyName to retrieve error for a specific property. There is no way to pass proposed value (that should be validated) to it, so only commited property value can be used.

    I consider this to be a good approach because view model and view are always synchronized, even if properties have invalid values and invalid value state is preserved in view model so additional logic, based on that information, can be contained in view model and not view.

    If you want to propagate proposed value validation to view model you are going to have to do it with your own custom interface and validation rule.

    Here is how I accomplished it:

    IProposedValueErrorInfo.cs

    using System.Globalization;
    
    namespace WpfApplication
    {
        public interface IProposedValueErrorInfo
        {
            object GetError(string propertyName, object value, CultureInfo cultureInfo);
        }
    }
    

    ProposedValueErrorValidationRule.cs

    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    
    namespace WpfApplication
    {
        internal sealed class ProposedValueErrorValidationRule : ValidationRule
        {
            private readonly DependencyObject targetObject;
            private readonly DependencyProperty targetProperty;
    
            public ProposedValueErrorValidationRule(DependencyObject targetObject, DependencyProperty targetProperty)
                : base(ValidationStep.RawProposedValue, true)
            {
                if (targetObject == null)
                    throw new ArgumentNullException("targetObject");
                if (targetProperty == null)
                    throw new ArgumentNullException("targetProperty");
    
                this.targetObject = targetObject;
                this.targetProperty = targetProperty;
            }
    
            public override ValidationResult Validate(object value, CultureInfo cultureInfo)
            {
                var expression = BindingOperations.GetBindingExpression(this.targetObject, this.targetProperty);
                if (expression != null)
                {
                    var sourceItem = expression.DataItem as IProposedValueErrorInfo;
                    if (sourceItem != null)
                    {
                        var propertyName = expression.ParentBinding.Path != null ? expression.ParentBinding.Path.Path : null;
                        if (propertyName != null)
                        {
                            var error = sourceItem.GetError(propertyName, value, cultureInfo);
                            if (error != null)
                                return new ValidationResult(false, error);
                        }
                    }
                }
                return ValidationResult.ValidResult;
            }
        }
    }
    

    ProposedValueValidationBindingExtension.cs

    using System;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Markup;
    
    namespace WpfApplication
    {
        public sealed class ProposedValueValidationBindingExtension : MarkupExtension
        {
            private readonly Binding binding;
    
            public ProposedValueValidationBindingExtension(Binding binding)
            {
                if (binding == null)
                    throw new ArgumentNullException("binding");
    
                this.binding = binding;
            }
    
            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                var provideValueTarget = serviceProvider != null ? serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget : null;
                if (provideValueTarget != null)
                    this.binding.ValidationRules.Add(new ProposedValueErrorValidationRule(provideValueTarget.TargetObject as DependencyObject, provideValueTarget.TargetProperty as DependencyProperty));
    
                return this.binding.ProvideValue(serviceProvider);
            }
        }
    }
    

    Person.cs

    using System.Globalization;
    
    namespace WpfApplication
    {
        public class Person : IProposedValueErrorInfo
        {
            public int Age { get; set; }
            public string Surname { get; set; }
    
            #region IProposedValueErrorInfo Members
    
            object IProposedValueErrorInfo.GetError(string propertyName, object value, CultureInfo cultureInfo)
            {
                switch (propertyName)
                {
                    case "Age":
                        int dummy;
                        return value is int || int.TryParse(value as string, NumberStyles.Integer, cultureInfo, out dummy) ? null : "Age must be a number.";
                }
    
                return null;
            }
    
            #endregion
        }
    }
    

    MainWindow.xaml

    <Window x:Class="WpfApplication.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication"
            Title="MainWindow" Height="350" Width="525">
        <Window.DataContext>
            <local:Person Age="16"/>
        </Window.DataContext>
        <StackPanel>
            <TextBox Text="{local:ProposedValueValidationBinding {Binding Age}}" ToolTip="{Binding Path='(Validation.Errors)/ErrorContent', RelativeSource={RelativeSource Self}}"/>
            <TextBox Text="{local:ProposedValueValidationBinding {Binding Age}}" ToolTip="{Binding Path='(Validation.Errors)/ErrorContent', RelativeSource={RelativeSource Self}}"/>
        </StackPanel>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.