我如何使用数据绑定Windows窗体单选按钮? [英] How do I use databinding with Windows Forms radio buttons?

查看:136
本文介绍了我如何使用数据绑定Windows窗体单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的数据库中的二进制字段,是很难在UI来描述使用一个单一的是XXXX? - 类型复选框。我宁愿使用一对单选按钮(例如做它的方式富和做它吧办法),但现在我所有的表单上的其他字段的数据绑定到业务对象。我想数据绑定对单选按钮业务对象的为好,但还没有想出了一个好办法做到这一点呢。我可以绑定按钮领域之一,使得如果选择了按钮,但同时选择的其他按钮便取消选择第一个(即,两个单选按钮被正确配对的字段被设定为真 ),该字段的值不更新,以反映这一点。

I have a binary field in my database that is hard to describe in a UI using a single "Is XXXX?"-type checkbox. I'd rather use a pair of radio buttons (e.g. "Do it the Foo way" and "Do it the Bar way"), but right now all the other fields on my form are data-bound to a business object. I'd like to data-bind the pair of radio buttons to the business object as well, but haven't come up with a good way to do it yet. I can bind one of the buttons to the field, such that the field is set "true" if the button is selected, but while selecting the other button does de-select the first one (that is, the two radio buttons are properly paired), the value of the field does not update to reflect this.

我想能说

button1.DataBindings.Add(new Binding("checked", source, "useFoo"));
button2.DataBindings.Add(new Binding("checked", source, "!useFoo"));



但我敢肯定它运行时将抛出。有没有更简单的方法,或者我应该只是把更多的心思研究如何词单个复选框?我不想增加额外的功能来处理这个事情琐碎的...

but I'm pretty sure that will throw when it runs. Is there an easier way, or should I just put more thought into how to word a single checkbox? I don't want to add extra functions to handle something this trivial...

ETA:一个评论者曾建议考虑下拉列表(组合框)。我曾想过这一点,但如何将我的数据绑定,要在业务对象的数据库/属性布尔型字段?如果我则selectedItem绑定到useFoo财产,你会在项集合去了?我必须补充一点,真与假,或者我能以某种方式补充说,系上显示的项目(使用富/不要用富),以它后面的布尔值的键/值对的对象? 。我无法找到这个文档

ETA: A commenter has suggested considering a dropdown (ComboBox). I had thought about this, but how would I data-bind that to a boolean field in a database/Property in a business object? If I bind the SelectedItem to the useFoo property, what would go in the Items collection? Would I have to add just "True" and "False", or could I somehow add a key/value pair object that ties a displayed item ("Use Foo" / "Do Not Use Foo") to the boolean value behind it? I'm having trouble finding docs on this.

关于回答:我清盘使用该解决方案涉及修改业务对象 - 基本思想是非常相似的一个张贴Gurge,但我单独想出了它之前,我看他的反应。总之,我增加了一个单独的属性,只返回!useFoo 。一个单选按钮被绑定到 source.UseFoo ,另一个必然会 source.UseBar (的名字新属性)。要确保新的属性都有getter和setter,否则你会用非常奇怪的行为拉闸这一点很重要。

About the answer: the solution I wound up using involved modifying the business object -- the basic idea is very similar to the one posted by Gurge, but I came up with it separately before I read his response. In short, I added a separate property that simply returns !useFoo. One radio button is bound to source.UseFoo, and the other is bound to source.UseBar (the name of the new property). It's important to make sure the new property has both getters and setters, or you'll wind up with really odd behavior.

推荐答案

我发现这样使用的DataSet / DataTable的一种方式。

I have found a way of doing this using DataSet/DataTable.

我在DataTable内使用表达式IIF(美孚=真,假,真)计算列。让我们把该列吧。

I make a calculated column in the DataTable with the expression IIF(Foo=true, false, true). Let's call that column Bar.

酒吧是布尔类型。现在,您可以绑定一条radiobutton.checked到Foo和一个酒吧。

Bar is of type Boolean. Now you can bind one radiobutton.checked to Foo and one to Bar.

要获得酒吧选中/取消选中传播回美孚必须转到生成的数据表的代码和添加一行,最后一个在此示例中:

To get Bar checking/unchecking to propagate back to Foo you must go to the generated DataTable code and add one line, the last one in this sample:

            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public bool Bar {
            get {
                try {
                    return ((bool)(this[this.tableradio.BarColumn]));
                }
                catch (global::System.InvalidCastException e) {
                    throw new global::System.Data.StrongTypingException("The value for column \'Bar\' in table \'radio\' is DBNull.", e);
                }
            }
            set {
                this[this.tableradio.BarColumn] = value;
                this[this.tableradio.FooColumn] = !value;
            }
        }

这篇关于我如何使用数据绑定Windows窗体单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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