开发者

WPF bind IsEnabled property to List's size

开发者 https://www.devze.com 2023-03-12 02:16 出处:网络
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开发者_如何学C that?Bin

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开发者_如何学C that?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消