I have a TextBlock
, I'd like to databind it to the count of a List<T>
. Sort-of.
I can databind it like this:
<TextBlock Name="tbAlerts" Text="{Binding Path=Alerts.Count}" />
where Alerts is a List<String>
, and it displays the correct thing. But, I'd like to display "No alerts" when the count is zero.
I thought a way to do this would be to extend List in order to expose an additional string property – call it CountText
– that emits the desired string. It might emit "No Alerts" when count is zero, and "One alert" when Count==1
. Will that work?
If I do this, how would I get the change in Count to result in a PropertyChanged
event for CountText
, so that it will get updated in the WPF UI?
is that the preferred way to get the effect I want?
Besides the Converter solution, you could also do it directly in Xaml by changing the
Text
property to "No Items" when you have no items in the listOne way to accomplish that is to create a IValueConverter that would return a string if the value is zero and/or any other number that you want to add custom text to. As for the updating the UI when the count changes, you will have to call the PropertyChanged handler on the list whenever an item is added/removed from the Alerts list.