C# - 我可以在值和表达式之间进行数据绑定吗?

发布于 2024-07-29 08:49:35 字数 182 浏览 3 评论 0原文

我有一个列表和一个按钮。 当列表计数 == 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凯凯我们等你回来 2024-08-05 08:49:35

创建一个 DTO(数据传输对象)来公开您想要绑定到 UI 元素的所有数据。 在 DTO 中创建一个属性(使用适当的名称):

public bool ButtonVisible
{
   get { return myListCount != 0; }
}

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):

public bool ButtonVisible
{
   get { return myListCount != 0; }
}

Add a BindingSource to your form and set it's DataSource 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..

友谊不毕业 2024-08-05 08:49:35

一般答案

编写一个事件处理程序并将其注册到列表控件的绑定对象

具体示例

class MyForm : Form {
protected Button myButton;
BindingSource myBindingSource;
DataGridView dgv;

public MyForm(List someList) {
    myBindingSource = new BindingSource();
    dgv = new DataGridView();
    this.myButton = new Button();
    this.Controls.Add(myButton);
    this.Controls.Add(dgv);

    myBindingSource.DataSource = someList;
    dgv.DataSource = myBindingSource;

    dgv.DataSource.ListChanged += new ListChangedEventHandler (ListEmptyDisableButton);
}

protected void ListEmptyDisableButton (object sender, ListChangedEventArgs e) {
    this.myButton.Visible = this.dgv.RowCount <= 0 ? false : true;          
}

}

PS - 我会投票否决最喜欢的答案。 数据传输对象 (DTO) 忽略了 .NET 绑定架构的全部要点和功能

The General Answer

Write an event handler and register it with your list-control's bindings object

A Specific Example

class MyForm : Form {
protected Button myButton;
BindingSource myBindingSource;
DataGridView dgv;

public MyForm(List someList) {
    myBindingSource = new BindingSource();
    dgv = new DataGridView();
    this.myButton = new Button();
    this.Controls.Add(myButton);
    this.Controls.Add(dgv);

    myBindingSource.DataSource = someList;
    dgv.DataSource = myBindingSource;

    dgv.DataSource.ListChanged += new ListChangedEventHandler (ListEmptyDisableButton);
}

protected void ListEmptyDisableButton (object sender, ListChangedEventArgs e) {
    this.myButton.Visible = this.dgv.RowCount <= 0 ? false : true;          
}

}

PS - I'd vote down the favorite answer. A Data Transfer Object (DTO) misses the whole point and functionality of .NET Binding architechture

当爱已成负担 2024-08-05 08:49:35

由于问题目前的措辞,听起来与 DataBind 没有任何关系。

如果我们有一个列表(无论它是通过代码填充还是绑定到数据源),我们都可以根据计数设置按钮的可见性。
例如

List<string> somelist = new List<string>();
somelist.Add("string1");
somelist.Add("string2");
Button1.Visible = somelist.Count > 0 ? true : false;

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.

List<string> somelist = new List<string>();
somelist.Add("string1");
somelist.Add("string2");
Button1.Visible = somelist.Count > 0 ? true : false;
不爱素颜 2024-08-05 08:49:35

我认为您想要使用控件的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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文