views:

173

answers:

3

Hi,

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.

+5  A: 

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..

Mitch Wheat
I don't think that your answer addresses the question completely - it only partially answers what the OP is trying to do.
Josh E
I don't think so Josh
ArsenMkrt
+1 upvoted after modifications
Josh E
A: 

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;
Mark Maslar
I don't want to do Button1.Visible = somelist.Count > 0 ? true : false; every time I add or remove something to / from the list.
ThePower
@ThePower why not?
Rex M
If it's achievable how I've asked then it would be much neater.
ThePower
A: 

I think you want to be using the CurrencyManager and the BindingContext of the control.

http://www.akadia.com/services/dotnet_databinding.html#CurrencyManager

CptSkippy