数据绑定单选按钮-从更改的源更新 [英] Databinding Radio buttons - Update from Changed Source

查看:96
本文介绍了数据绑定单选按钮-从更改的源更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在为公司的示例客户端工作,并且试图使用数据绑定在Winform UI中启用/禁用控件.

我有一个填充的对象结构,可以对其进行解析并动态创建控件(标签,单选按钮,复选框,文本框等),然后将其插入到表单中.

在创建时,我能够设置数据绑定,以便控件使用该对象的"IsAvailable"属性,并使用该属性控制控件上的"Enabled"属性,以便在最初绘制表单时,控件需要禁用的控件不可用,而需要启用的控件可用.

我还设置了数据绑定,以在更改控件(选中复选框,选择按钮等)时更新源,所有这些都可以正常使用.

问题是,一些残疾人控制需要当选择一些其他的控制在UI启用.

示例:选中单选按钮A时,应启用Checkbox1至Checkbox10.

后面的代码正确设置了表明复选框应该可用的对象的属性,并且正在发生的事件通知客户端发生了这些更改,但是我无法使用数据绑定来强制具有更新属性的控件来实际上在Windform UI中反映了已启用的状态更改.

我试过了:

Hi all,

I''m working on a sample client for my company and I''m trying to use databinding for enabling/disabling controls in the Winform UI.

I have a populated object structure that I parse through and dynamically create controls (Labels, Radiobuttons, Checkboxes, Textboxes, etc.) and insert them onto the form.

At the time of creation, I am able to set databinding so that the control honors the object''s "IsAvailable" property and use that to control the "Enabled" property on the control, so that when the form draws initially, the controls that need to be disabled are not available while the controls that need to be enabled are available.

I have also set a databinding to update the source when a control is changed (Box checked, button selected, etc.) and all of this works perfectly.

The problem is that some of the disabled controls need to be enabled in the UI when some other control is selected.

Example: Radiobutton A, when selected, should enable Checkbox1 through Checkbox10.

The code behind is properly setting the property in the object that says the checkboxes should be available, and the events are happening that inform the client that these changes happened, but I''m unable to use databinding to force the controls with updated properties to actually reflect that enabled state change in the Windform UI.

I tried this:

textbox.DataBindings.Add(new Binding("Enabled", answer, "IsAvailable", true));
textbox.DataBindings[0].ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;



但是,它不会即时调用UI更新.我还尝试使用change事件浏览UI中的每个控件并刷新每个控件,但它仍然不接受数据源中会更改控件的启用状态的任何更改.

我找不到能将控件重新绑定"到数据源以强制执行UI更新的方法,因此我想知道是否有人知道一种使这项工作像WPF一样简单的方法(其中我的工作顺利).

任何指示或建议,将不胜感激.

{Update}

我应该告诉你,我当前的解决方案是使用Control.Tag存储对控件表示的数据对象的引用,当我收到更改通知时,我将遍历UI控件,然后将数据对象引用拉出并使用它来设置控件的可用性.

像这样:

([[CheckBox] control).Enabled = [[Answer] [[CheckBox] control).Tag).IsAvailable;

但是,对我而言,这是一种可以解决数据绑定问题的技巧,因此,如果有其他解决方案,我将很高兴听到它们.

再次感谢!

谢谢!
Mike Poz



However it does not invoke a UI update on the fly. I also tried using the change events to step through every control in the UI and refresh each and it still doesn''t honor any changes in the datasource that would alter the control''s enabled state.

There is no method that I can find, method to "rebind" a control to a datasource to force the UI update and so I was wondering if, perhaps, someone knows of a way to make this work that is as simple as WPF (where I have it working smoothly).

Any pointers or suggestions would be greatly appreciated.

{Update}

I should tell you that my current solution is to use Control.Tag to store a reference to the data object the control represents, and when I get the notification of a change, I walk through the UI controls and then pull the data object reference out and use that to set the control''s availability.

Like this:

((CheckBox)control).Enabled = ((Answer)((CheckBox)control).Tag).IsAvailable;

However this, to me, is a hack that databinding should be able to resolve, so if there are any other solutions out there, I would be happy to hear of them.

Thanks again!

Thanks!
Mike Poz

推荐答案

你好

使用ObservableCollection<T>代替List<T>
在Windows应用程序中有可能,您应该在引用中添加WindowsBase进行访问.
有关此集合的更多信息,请参见: ObservableCollection< t>类

然后,您必须实现INotifyPropertyChanged interface.
看一下: INotifyPropertyChanged接口

然后,您可以定义绑定以执行一些事件.例如启用控件.

ObservableCollection< t>在Winforms和可能的替代方案中
Hello

Use ObservableCollection<T> instead of List<T>
It''s possible in Windows application, you should add WindowsBase to the references to access.
For more information about this collection look at this: ObservableCollection<t> Class

Then you must implement the INotifyPropertyChanged interface.
Look at this: INotifyPropertyChanged Interface

Then you can define bindings to do some events. For example enabling the controls.

ObservableCollection<t> in Winforms and possible alternatives


这篇关于数据绑定单选按钮-从更改的源更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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