简体   繁体   English

使用Winforms数据绑定检测脏

[英]Detecting dirty using winforms data binding

I am using 2 way binding with winforms text boxes. 我正在使用Winforms文本框的2种方式绑定。 I need to work out if the user has changed my data Looking at the help for 我需要确定用户是否更改了我的数据。

the CurrentItemChanged Event CurrentItemChanged事件

It seems that this event does fire if a property has changed, however it also fires if current has changed. 似乎如果属性已更改,则触发此事件,但是如果电流已更改,则也会触发。

Is there a way to tell whether the data has changed? 有没有办法判断数据是否已更改?

a similar question is also asked here but not answered in my opinion 这里也问了类似的问题,但我认为没有回答

Oliver mentions "if your object within the List support the INotifyPropertyChanged event and you replace the List by a BindingList you can subscribe to the ListChanged event of the BindingList to get informed about any changes made by the user." 奥利弗提到:“如果列表中的对象支持INotifyPropertyChanged事件,并且用BindingList替换列表,则可以订阅BindingList的ListChanged事件,以获取有关用户所做的任何更改的信息。”

My application meets these conditions but I cant get this working. 我的应用程序满足这些条件,但无法正常工作。 The ListChangedType.ItemChanged property looked hopeful, but it changes when I navigate to the next record without changing the data ListChangedType.ItemChanged属性看起来很有希望,但是当我导航到下一条记录而不更改数据时,它会更改

I found a link at Microsoft here but surely it cant be that hard! 在这里找到了Microsoft的链接但是肯定不会那么难!

This seems to work 这似乎有效

void bindingSource_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate)
            {
                var person = (Person)bindingSource.Current;

                if ( person.State == State.Unchanged && (e.BindingCompleteState == BindingCompleteState.Success)
                && e.Binding.Control.Focused)
                {
                    person.State = State.Modified;  // using Julie Lerman's repositories technique
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM