以编程方式设置绑定属性作为另一个控件中选定值的结果
我有 ac# winforms 应用程序,其中包含许多不同的控件,这些控件(通过绑定源)绑定到我的业务对象的不同属性(并且该对象实现了 INotifyPropertyChanged)。当我的用户将某个控件设置为特定值时,我需要更改另一个属性的值(在代码中),甚至可能禁用绑定到该属性的控件。例如,我有一个映射到日期时间属性的日期时间选择器。如果我的用户输入的时间值小于 00:10:00 (HH:MM:SS),我需要取消选中复选框(绑定到 bool 属性)。目前,我在验证事件处理程序中有代码,该代码查看日期时间选择器中的值以及 .Hour == 0 和 .Minute < 是否为 0。 10、我以编程方式将 bool 属性(复选框绑定到的)值设置为 false。当我执行此操作时,该复选框将变为未选中状态,但 datetimepicker 中的值将恢复为用户输入导致布尔属性首先设置为 false 的时间之前的值。
如果我在调试器中观察 datetimepicker 的值,它会在执行这行代码后立即重置:
businessObject.booleanVariable = false;
在以编程方式将 boolean 属性设置为 false 之前,我是否需要执行某些操作来强制将 datetimepicker 值返回到对象属性? !?或者我对这一切都错了?是否有其他方法可以处理可能影响其他绑定控件的绑定控件?
I have a c# winforms app with a lot of different controls that are bound (via bindingsource) to different properties of my business object (and this object implements INotifyPropertyChanged). When my user sets a certain control to a particular value, I need to change the value of another property (in code) and perhaps even disable the control that binds to that property. For example, I have a datetimepicker that maps to a datetime property. If my user enters a time value less than 00:10:00 (HH:MM:SS), I need to uncheck a checkbox (which is bound to a bool property). Currently, I have code in a validating event handler that looks at the value in the datetimepicker and if the .Hour == 0 and the .Minute < 10, I programatically set the value of the bool property (that the checkbox is bound to) equal to false. When I do this, the checkbox becomes unchecked, but the value in the datetimepicker reverts to what it was before the user entered the time that caused the boolean property to be set to false in the first place.
If I watch the value of the datetimepicker in the debugger, it gets reset as soon as this line of code executes:
businessObject.booleanVariable = false;
Do I need to do something to force the datetimepicker value back to the object property before I programatically set the boolean property to false?!? Or am I going about this all wrong? Is there some other way that bound controls which may impact other bound controls should be handled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Validating
事件在数据发送回绑定对象之前触发。其目的是让您确保控件包含根据业务逻辑有效的数据,并且使用它来处理普通(非验证)逻辑将导致您所看到的问题。最好的做法是在属性的 setter 中添加代码来设置
bool
属性。The
Validating
event fires before data is sent back to the bound object. Its purpose is to allow you to ensure that a control contains data that is valid according to business logic, and using it for handling ordinary (non-validation) logic will cause problems like you're seeing.The best course of action would be to add code in the setter of your property that sets the
bool
property.