redman
redman

Reputation: 2165

WPF bind IsEnabled property to List's size

I want to bind the IsEnabled property (of a ribbon button) to a lists size. So when lists size is > 0 then IsEnabled is set to true else (if 0) it's set to false. How do you do that?

Upvotes: 2

Views: 2451

Answers (2)

HCL
HCL

Reputation: 36785

Either do it with a DataTrigger that binds to the Count property of the list and sets IsEnabled to false if it is zero, or use a ValueConverter.

However take care, that a List<T> does not implement INotifyPropertyChanged, which informs about changes of the Count property. An ObservableCollection<T> will do.

Upvotes: 2

ChrisWue
ChrisWue

Reputation: 19020

Bind to the lists Count property and create your own ValueConverter to convert from an int to a bool (in your case returning true if the int is larger than 0 and false otherwise). Note that your list would need to raise a PropertyChanged event when the count changes - ObservableCollection does that for example.

Upvotes: 3

Related Questions