简体   繁体   English

验证绑定到同一对象的不同属性的控件会重置对象值

[英]Validating controls bound to different properties of the same object resets objectvalues

I have a form on which all textboxes are bound to different properties of the same dataobject that implements INotifyPropertyChanged. 我有一个表单,所有文本框都绑定到实现INotifyPropertyChanged 的同一数据对象的不同属性。 The Forms Autovalidate is set to 'Disable' as I want to trigger the validation explicitly by calling form.ValidateChildren() . Forms Autovalidate设置为'Disable',因为我想通过调用form.ValidateChildren()显式触发验证。

Expected: After calling ValidateChildren all edited values should be in my dataobject. 预期:在调用ValidateChildren之后,所有编辑的值都应该在我的数据对象中。

Problem: Only the last focused control writes it's data to the dataobject, but all other controls lose the edited values and show the old value instead. 问题:只有最后一个聚焦控件将其数据写入数据对象,但所有其他控件都会丢失已编辑的值并显示旧值。

Question: How can I make sure that all data is validated before the controls refresh themselves? 问题:如何在控件刷新之前确保所有数据都已经过验证?

Using Autovalidate = EnablePreventFocusChange or EnableAllowFocusChange does work but as I want to validate all at once it is not an acceptable solution for me. 使用Autovalidate = EnablePreventFocusChange或EnableAllowFocusChange确实有效,但因为我想立即验证所有这些对我来说不是一个可接受的解决方案。

Searching the internet for soutions I found an example showing the same problem but unfortunately no solution. 在互联网上搜索我发现了一个显示同样问题的例子 ,但遗憾的是没有解决方案。

EDIT After further investigation i tried this and it works: 编辑经过进一步调查我试过这个,它的工作原理:

form.BindingContext[dataobject].SuspendBinding();
form.ValidateChildren();
form.BindingContext[dataobject].ResumeBinding();

Is Pausing the Binding the standard way or are there any better solutions to fix this? 是暂停绑定的标准方式还是有更好的解决方案来解决这个问题?

I'm not sure if this is helpful or will it be a complete answer but I had similar problem where only one value has been saved out of whole form. 我不确定这是否有用,或者它是一个完整的答案,但我有类似的问题,只有一个值已经从整个表格中保存。 Here is some background explanation I found on the MS site: 以下是我在MS网站上发现的一些背景说明:

http://connect.microsoft.com/VisualStudio/feedback/details/351146/binding-writevalue-does-not-update-underlying-data-source http://connect.microsoft.com/VisualStudio/feedback/details/351146/binding-writevalue-does-not-update-underlying-data-source

Even SuspendBinding didn't work for me. 甚至SuspendBinding也不适用于我。 So following the link and the information there I connected the objects via BindingSource object with RaiseListChangeEvents property set to False 因此,根据链接和信息,我通过BindingSource对象连接对象,并将RaiseListChangeEvents属性设置为False

Dim MyBindingSource = New BindingSource With {.DataSource = MyDataSource, .RaiseListChangedEvents = False}
MyControl.DataSource = MyBindingSource

Once this is in place you can call refer to the binding on the control and write the value manually (assuming there is just one binding) the code will look similar to: 一旦这就到位,你可以调用引用控件上的绑定并手动写入值(假设只有一个绑定)代码看起来类似于:

MyControl.Bindings(0).WriteValue()

Let me know if this helps. 如果这有帮助,请告诉我。

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

相关问题 通过其绑定属性获取XAML控件? - Get XAML controls by their bound properties? 如何设置.NET Win App控件的大小属性在不同系统上具有相同的外观? - how to set .NET win app controls size properties to have same looking on different systems? 基于绑定对象和模型属性的可见绑定 - Visible Binding Based On Bound Object and Model Properties 使用数据注释验证绑定到 SelectList 对象的下拉列表 - Validating a dropdown bound to a SelectList object using data annotations 将JSON序列化为对象。 有些属性总是一样,有些则不同 - Dserialise JSON to object. Some properties always the same, some different 如何使用对象初始化器将相同的值分配给不同的属性 - How to assign a same value to different properties using Object Initializer IValueConverter的绑定值重置为默认值 - Bound value for IValueConverter resets to default 绑定TabControl重复相同的内容或跨选项卡控制 - Bound TabControl Repeats Same Content Or Controls Across Tabs WPF-绑定到同一控件实例的两个用户控件的内容 - WPF - Content of two user controls bound to same instance of control 将Windows窗体控件绑定到对象的属性 - Binding windows form controls to an object's properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM