将一个属性绑定到另一个属性
我有嵌套的组框,它们在逻辑上表示我的应用程序中的嵌套数据结构。 假设我有一个这样的结构:
Dev1
- CDev1
- CDev2
- ICDev1
- ICDev2
我有复选框来启用/禁用每个开发人员。 我想将子复选框的 CheckState 绑定到父复选框的 CheckState。 我希望该机制像这样工作:当我检查 CDev2、ICDev1 & ICDev2 会自动检查。 但是当我取消选中 ICDev1 时,CDev2 保持其自身状态。 基本上,我希望这些事件传播给孩子而不是父母,就像单向绑定一样。
我正在使用.Net 2.0 SP2。 我不知道这是否可行,因此如果您向我展示一些有关此问题的指示,我将很高兴。 如果不可能,我将为所有复选框实现事件处理程序。
I have nested groupboxes, which logically represent nested data structures in my application. Let's say i have a structure like this:
Dev1
- CDev1
- CDev2
- ICDev1
- ICDev2
I have checkboxes to enable/disable each of these Devs. I want to bind the CheckState of the child checkboxes to the parent checkbox's CheckState. I want the mechanism to work like this: When i check CDev2, ICDev1 & ICDev2 get automatically checked. But when I uncheck ICDev1, CDev2 stays in its own state. Basically, i want these events to be propagated to children but not to parent, like one way binding.
I am using .Net 2.0 SP2. I don't know if this is possible or not, therefore i would be glad if you show me some pointers about this. If it's not possible, i am going to implement event handlers for all checkboxes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
您可能还想看看
TreeView
控件,它可以选择在每个项目旁边显示CheckBox
。Try this:
You might also want to take a look at the
TreeView
control, it has the option to displayCheckBox
next to each item.理论上,在 WPF 中使用双向绑定是可能的(这里有一个关于数据绑定的教程在 WPF 中)。 我认为您不能使用 WinForms 自动完成此操作。 如果您使用 WinForms,则必须捕获并处理事件以手动修改此类状态更改。
This is theoretically possible using two-way binding in WPF (here's a tutorial on data binding in WPF). I don't think you can do it automatically using WinForms. If you're using WinForms, you'll have to catch and handle events to manually modify state changes of that sort.
绑定并不能解决你的问题。 如果您找到一种将 ICDev1 和 ICDev2 绑定到 CDev2 的方法,则意味着当检查 ICDev1 时,ICDev1 和 ICDev2 都会被检查,反之亦然。 如果这就是您想要的,则不需要 ICDev1、ICDev2 的复选框,仅需要 CDev2 的复选框。 如果您希望用户选中 ICDev1,但取消选中 ICDev2,则需要实现事件处理程序。
Binding won't solve your problem. If you find a way to bind ICDev1 and ICDev2 to CDev2, that will mean that when ICDev1 is checked, both ICDev1 and ICDev2 get checked and vice versa. If that is what you want, you don't need checkboxes for ICDev1, ICDev2, only for CDev2. If you want user to be check ICDev1, but uncheck ICDev2, you need to implement event handlers.
在WPF中是可能的,但我认为在winforms中不可能,因为WinForms中没有像依赖属性或INotifyPropertyChanged这样的属性更新事件机制
It is possible in WPF, but I don't think it is possible in winforms because there is no property update event mechanism like dependency property or INotifyPropertyChanged in WinForms
请查看此处和链接到 Kent Boogart 的 Truss 库,它完全绕过了 winforms。 不过,它可能需要一些子类化,具体取决于您的模型,因为它的工作方式类似于 wpf 绑定。
Take a look here, and the link to the Truss library by Kent Boogart, which circumvents winforms altogether. It may require some subclassing though, depending on your model, since it works similar to wpf binding.