WPF 4.5 DataGrid 的 INotifyDataErrorInfo 是否已损坏

发布于 2024-12-11 18:31:43 字数 7843 浏览 0 评论 0原文

我在 WPF 4.5 项目中简单实现了 INotifyDataErrorInfo。这是 WPF 的新界面,但在 Silverlight 中已经可用一段时间了。

我知道 NET4.5 仍被认为是 alpha,但我正在尝试确定是我的代码还是框架有问题。

该接口按预期工作,但当对象绑定到 DataGrid 时会失败。

我收到的异常是:

System.NullReferenceException 未被用户代码处理
消息=未将对象引用设置为对象的实例。
来源=PresentationFramework StackTrace: 在 MS.Internal.Data.ClrBindingWorker.OnDataErrorsChanged(INotifyDataErrorInfo indei,字符串 propName) 在 MS.Internal.Data.PropertyPathWorker.OnErrorsChanged(对象发送者,DataErrorsChangedEventArgs e) 在 System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(对象 发送者、EventArgs e、类型管理器类型) 在 System.Windows.WeakEventManager.DeliverEvent(对象发送者,EventArgs args) 在 System.ComponentModel.ErrorsChangedEventManager.OnErrorsChanged(对象 发送者,DataErrorsChangedEventArgs 参数) 在 INotifyDataErrorInfoTest\Person.cs 中的 INotifyDataErrorInfoTest.Person.NotifyErrorsChanged(字符串属性):第 109 行 在 INotifyDataErrorInfoTest.Person.AddErrorForProperty(String property, String error) 处 INotifyDataErrorInfoTest\Person.cs:第 122 行 在 INotifyDataErrorInfoTest\Person.cs 中的 INotifyDataErrorInfoTest.Person.Validate(String propertyName):第 150 行 位于 INotifyDataErrorInfoTest\Person.cs 中的 INotifyDataErrorInfoTest.Person.set_FirstName(String value):第 18 行

代码位于下面或位于 http://dl.dropbox.com/u/14740106/INotifyDataErrorInfoTest.zip

如果一致认为这是一个错误,那么我将发布到 MS Connect。

测试: 有两个文本框绑定到 Person 对象的单个实例。将第一个文本框的值设置为 James,它将验证失败并显示红色框。如果将网格中任何用户的名字设置为 James,则会引发异常。

PS:我知道这不是MVVM,但它只是为了证明或反驳问题。

 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少年亿悲伤 2024-12-18 18:31:43

答案是肯定的。我打开了一张 票证Microsoft 和他们已经确认代码没有问题,这是 .NET 4.5 DataGrid 的一个错误。

这是我们的错误,不是你的。当您编辑 DataGrid 单元格时,DataGrid 会放弃“显示”模板的绑定,并将其替换为“编辑”模板的绑定。丢弃的绑定应停止侦听 INDEI.ErrorsChanged 事件。他们没有(这就是错误),但他们不准备再接受该事件。当事件到达时,会发生空引用崩溃。

这将在最终版本中修复。感谢您发现并报告它。

相当大的错误,必须等到最终版本。我们希望它能在下一个版本中得到修复。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文