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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:13:16+00:00 2026-05-26T18:13:16+00:00

I have made a simple implementation of INotifyDataErrorInfo in a WPF 4.5 project. This

  • 0

I have made a simple implementation of INotifyDataErrorInfo in a WPF 4.5 project. This is a new interface for WPF but has been available in Silverlight for some time.

I know that NET4.5 is still considered alpha but I’m trying to work out if it is my code or the framework at fault.

The interface works as expected but fails when an object is bound to a DataGrid.

The exception I receive is:

System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=PresentationFramework StackTrace:
at MS.Internal.Data.ClrBindingWorker.OnDataErrorsChanged(INotifyDataErrorInfo
indei, String propName)
at MS.Internal.Data.PropertyPathWorker.OnErrorsChanged(Object sender, DataErrorsChangedEventArgs e)
at System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object
sender, EventArgs e, Type managerType)
at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args)
at System.ComponentModel.ErrorsChangedEventManager.OnErrorsChanged(Object
sender, DataErrorsChangedEventArgs args)
at INotifyDataErrorInfoTest.Person.NotifyErrorsChanged(String property) in INotifyDataErrorInfoTest\Person.cs:line 109
at INotifyDataErrorInfoTest.Person.AddErrorForProperty(String property, String error) in INotifyDataErrorInfoTest\Person.cs:line 122
at INotifyDataErrorInfoTest.Person.Validate(String propertyName) in INotifyDataErrorInfoTest\Person.cs:line 150
at INotifyDataErrorInfoTest.Person.set_FirstName(String value) in INotifyDataErrorInfoTest\Person.cs:line 18

The code is below or in project at http://dl.dropbox.com/u/14740106/INotifyDataErrorInfoTest.zip

If consensus is that this is a bug then I will post to MS Connect.

Testing:
There are two textboxes bound to a single instance of a Person object. Set the first textbox to have a value of James and it will fail validation and show the red box. If you set the first name of any user in the grid to James the exception will be thrown.

PS: I know it is not MVVM but it is just to prove or disprove the problem.

 public class Person : INotifyDataErrorInfo, INotifyPropertyChanged
    {
        string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set
            {
                _firstName = value;
                Validate("FirstName");
                OnPropertyChanged("FirstName");
            }
        }

        string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set
            {
                _lastName = value;
                Validate("LastName");
                OnPropertyChanged("LastName");
            }
        }

        public Person()
        {
        }

        public Person(string first, string last)
        {
            this._firstName = first;
            this._lastName = last;
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Event to indicate that a property has changed.
        /// </summary>
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Called when a property is changed
        /// </summary>
        /// <param name="propertyName">The name of the property that has changed.</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// Called when a property is changed
        /// </summary>
        /// <param name="e">PropertyChangedEventArgs</param>
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {

            //Validate the property
            Validate(e.PropertyName);

            if (null != PropertyChanged)
            {
                PropertyChanged(this, e);
            }

        }

        #endregion

        #region INotifyDataErrorInfo Members

        public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
        private Dictionary<string, List<string>> _errors = new Dictionary<string, List<string>>();

        public IEnumerable GetErrors(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return (_errors.Values);
            }

            MakeOrCreatePropertyErrorList(propertyName);
            return _errors[propertyName];
        }

        public bool HasErrors
        {
            get
            {
                return (_errors.Where(c => c.Value.Count > 0).Count() > 0);
            }
        }

        void NotifyErrorsChanged(string property)
        {
            if (ErrorsChanged != null)
            {
                ErrorsChanged(this, new DataErrorsChangedEventArgs(property));
            }
        }
        public void ClearErrorFromProperty(string property)
        {
            MakeOrCreatePropertyErrorList(property);
            _errors[property].Clear();
            NotifyErrorsChanged(property);
        }
        public void AddErrorForProperty(string property, string error)
        {
            MakeOrCreatePropertyErrorList(property);
            _errors[property].Add(error);
            NotifyErrorsChanged(property);
        }

        void MakeOrCreatePropertyErrorList(string propertyName)
        {
            if (!_errors.ContainsKey(propertyName))
            {
                _errors[propertyName] = new List<string>();
            }
        }

        #endregion

        /// <summary>
        /// Force the object to validate itself using the assigned business rules.
        /// </summary>
        /// <param name="propertyName">Name of the property to validate.</param>
        public void Validate(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                return;
            }

            if (propertyName == "FirstName")
            {
                if (FirstName == "James")
                {
                    AddErrorForProperty(propertyName, "FirstName can't be James");
                }
                else
                {
                    ClearErrorFromProperty(propertyName);
                }
            }
        }
    }

public class NameList : ObservableCollection<Person>
    {
        public NameList()
            : base()
        {
            Add(new Person("Willa", "Cather"));
            Add(new Person("Isak", "Dinesen"));
            Add(new Person("Victor", "Hugo"));
            Add(new Person("Jules", "Verne"));
        }
    }

   public partial class MainWindow : Window
    {

        Person _person = new Person();

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        public Person Person
        {
            get { return _person; }
        }
}

<Window x:Class="INotifyDataErrorInfoTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:INotifyDataErrorInfoTest"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <c:NameList x:Key="NameListData"/>
    </Window.Resources>
    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="TextBox">
                <Setter Property="Margin" Value="5"></Setter>
            </Style>
        </StackPanel.Resources>
        <TextBox Text="{Binding Person.FirstName, Mode=TwoWay, ValidatesOnNotifyDataErrors=True}"/>
        <TextBox Text="{Binding Person.LastName, Mode=TwoWay, ValidatesOnNotifyDataErrors=True}"/>
        <TextBlock>To generate an error, set the FirstName of any row to James.
        </TextBlock>
        <DataGrid ItemsSource="{Binding Source={StaticResource NameListData}}" AutoGenerateColumns="True"></DataGrid>
    </StackPanel>
</Window>
  • 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-26T18:13:17+00:00Added an answer on May 26, 2026 at 6:13 pm

    The answer is YES. I opened a ticket with Microsoft and they have confirmed that the code is fine and it is a bug with the .NET 4.5 DataGrid.

    This is our bug, not yours. When you edit a DataGrid cell, the DataGrid discards bindings for the “display” template and replaces them with bindings for the “edit” template. The discarded bindings should stop listening to the INDEI.ErrorsChanged event. They don’t (that’s the bug), but they are not prepared to get the event any more. When the event arrives, a null-reference crash occurs.

    This will be fixed in the final release. Thanks for finding and reporting it.

    Pretty big bug to have to wait until the final release. Let’s hope it gets fixed by the next release.

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

Sidebar

Related Questions

I'm just playing around with WPF and MVVM, and I have made a simple
I have been playing around a bit with a fairly simple, home-made search engine,
I have a class that implements an interface, which is made available for plugins.
I have made a simple app to try a nut out the problem; this
I have made a simple test application for the issue, two winforms each containing
I have made my own file type (.ddd) and I made a simple program
I have started using jQuery and rails. I have made a simple form that
I have made a very simple AppleScript to close tabs in Safari. The problem
I have a simple nav made up of the following HTML; <div class=inner> <div
in my application I made a very simple binding. I have a NSMutableArray bound

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.