C# - 我可以在值和表达式之间进行数据绑定吗?
我有一个列表和一个按钮。 当列表计数 == 0 时,我希望按钮可见性 = false。
如何使用数据绑定来做到这一点?
预先感谢,
已添加
我问这个问题是为了避免每次在列表中添加或删除项目时在代码中检查列表中的计数。 但如果没有解决办法,我就会继续这样做。
I have a List and a Button. When the Lists Count == 0, I would like the button Visibility to = false.
How do I do this using Data Binding?
Thanks in advance,
Added
I have asked this so that I can try to avoid checking the Count on the list in code every time I add or remove an item to or from the list. But if there is no solution then I will continue to do it that way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建一个 DTO(数据传输对象)来公开您想要绑定到 UI 元素的所有数据。 在 DTO 中创建一个属性(使用适当的名称):
将
BindingSource
添加到您的表单,并将其DataSource
设置为您的 DTO 类型。单击按钮,转到属性。 展开DataBindings节点,然后单击高级。
向下滚动左侧窗格中的列表,然后选择可见。 将其绑定到通过 BindingSource 公开的属性。
Create a DTO (Data Transfer Object) that exposes all your data that you intend to bind to UI elements. Create a property in the DTO (with an appropriate name):
Add a
BindingSource
to your form and set it'sDataSource
to your DTO type.Click on the Button, goto Properties. Expand the DataBindings node, and click Advanced.
Scroll down the list in the left hand pane, and select Visible. Set it's binding to your property exposed vis the BindingSource..
一般答案
编写一个事件处理程序并将其注册到列表控件的绑定对象
具体示例
}
PS - 我会投票否决最喜欢的答案。 数据传输对象 (DTO) 忽略了 .NET 绑定架构的全部要点和功能
The General Answer
Write an event handler and register it with your list-control's bindings object
A Specific Example
}
PS - I'd vote down the favorite answer. A Data Transfer Object (DTO) misses the whole point and functionality of .NET Binding architechture
由于问题目前的措辞,听起来与 DataBind 没有任何关系。
如果我们有一个列表(无论它是通过代码填充还是绑定到数据源),我们都可以根据计数设置按钮的可见性。
例如
As the question is currently worded, it doesn't sound like it has anything to do w/ DataBind.
If we have a list -- doesn't matter whether it's populated via code or bound to a data source -- we can set the button's visibility based on the count.
e.g.
我认为您想要使用控件的CurrencyManager 和BindingContext。
http://www.akadia.com/services/dotnet_databinding.html#CurrencyManager
I think you want to be using the CurrencyManager and the BindingContext of the control.
http://www.akadia.com/services/dotnet_databinding.html#CurrencyManager