无法从数据绑定 Winforms 下拉列表中跳出 [英] Cannot tab out of databound Winforms dropdown list

查看:25
本文介绍了无法从数据绑定 Winforms 下拉列表中跳出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点奇怪,但我已经挣扎了几个小时了,我无法理解发生了什么.

This is a bit of a strange one, but I've been struggling for a few hours now and I can't understand what is happening.

我想知道是否有其他人遇到过这个问题,也许可以解释一下.我正在构建一个简单的 Winforms 应用程序并尝试使用许多内置控件.

I was wondering if anyone else has experienced this problem, and can perhaps explain it. I'm building a simple Winforms app and trying to use many of the built in controls.

基本上,我有一个带有用户控件和一些数据捕获字段的表单.其中 3 个字段是下拉列表,在用户控件上我有一个直接绑定到 Product 类的 bindingSource 控件.

Basically, I've got a form with a user control and some data capture fields. 3 of the fields are dropdown lists and on the user control I have a bindingSource control that binds directly to a Product class.

在运行时,我向 BindingSource 提供 Product 类的实例,并且该类包含 ProductType 的属性.为简单起见,我还在产品类中添加了一个 List<ProductType> ProductTypes,该类在查询时会自行加载,这意味着我可以使用相同的 bindingSource 并选择 ProductTypes 数据成员作为下拉列表的数据源.

At run time I provide an instance of the Product class to the BindingSource and the class contains a property of ProductType. For simplicity I also added a List<ProductType> ProductTypes to the Product Class which loads itself when queried, which means I can just use the same bindingSource and choose the ProductTypes Data Member as the Datasource for the dropdownlist.

运行表单后,列表完美绑定,我可以看到列出的所有产品类型,我可以选择一个并使用标签或单击下一个字段.但显然选定的值不会绑定,因为我没有为下拉列表选择任何绑定-SelectedValue,只有一个数据源.只要我确保下拉通过绑定到 Bindings-SelectedValue 来修改 Product 的实例,然后运行表单,列表仍然会完美填充,只要我不这样做,我就可以通过控件进行选项卡从下拉列表中进行选择.如果我从下拉列表中进行选择,则下拉列表保持焦点.我无法为爱情或金钱选择标签,甚至无法单击表单上的取消按钮,右上角的关闭按钮是我可以单击的唯一有效按钮,我无法单击任何其他字段或下拉菜单.一旦做出选择,这将影响所有三个下拉菜单.

Upon running the form, the list binds perfectly and I can see all the product types listed, and I can select one and tab or click to the next field. But obviously the selected value won't bind because I've not chosen any bindings-SelectedValue for the dropdown, only a datasource. As soon as I make sure that the drop down modifies the instance of the Product by binding to the Bindings-SelectedValue, and then run the form, the list still gets populated perfectly and I can tab through the controls as long as I don't make a selection from the dropdown. If I make a selection from the dropdown then the dropdown holds focus. I cannot tab out for love or money and can't even click cancel button on the form, the close button top right is the only button I can click which works and I can't click any other field or dropdown. This affects all three dropdowns as soon as a selection is made.

有人知道我缺少什么吗?

Anyone have any ideas what I'm missing?

我已经尝试改变一些东西,并通过将下拉值提供一个字符串 [] 而不是对象的成员来取得一些成功.这似乎可行,但破坏了使用数据绑定的对象,不是吗?

I have tried changing a few things and had some success by feeding the dropdown values a string[] instead of a member of an object. That seems to work, but defeats the object of using databinding doesn't it?

任何帮助表示赞赏!

推荐答案

这里只是猜测,因为我现在没有时间设置测试和确认,但是你在做任何验证吗?我似乎记得如果内容不验证,数据绑定控件不会让你离开.即使您没有明确表示,也可以尝试将 CausesValidation 设置为 False,以查看是否有任何类型的验证在幕后进行,这至少会给您一个提示.

Just guessing here, because I don't have time to set up a test and confirm right now, but are you doing any validating? I seem to remember that data-bound controls won't let you leave if the contents don't validate. Even if you aren't explicitly, try setting CausesValidation to False to see if there's any sort of validation going on behind the scenes, that might at least give you a hint.

这篇关于无法从数据绑定 Winforms 下拉列表中跳出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆