Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of items in ObservableCollection from XAML?

I'm displaying all of my customers which I get from a ViewModel ObservableCollectoin property within a ComboBox like this:

<ComboBox 
    ItemsSource="{Binding Customers}"
    ItemTemplate="{StaticResource CustomerComboBoxTemplate}"
    Margin="20"
    HorizontalAlignment="Left"
    SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"/>

Is there a way to get the number of items in the ObservableCollection without creating another ViewModel property, e.g. something like this:

PSEUDO-CODE:

<TextBlock Text="{Binding Customers.Count()}"/>
like image 865
Edward Tanguay Avatar asked Jun 18 '09 12:06

Edward Tanguay


People also ask

How does ObservableCollection work?

An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated.

What is Observable collection in xamarin?

Represents a dynamic data collection that provides notifications when items get added or removed, or when the whole list is refreshed.


1 Answers

The ObservableCollection type exposes a Count Property which you can use. I don't know if ObservableCollection raises the PropertyChanged event in order to inform the UI about updates to this property though.

like image 101
Joachim Kerschbaumer Avatar answered Sep 29 '22 20:09

Joachim Kerschbaumer