I have a small WPF application in which I have a list of files. Below the list of files I have an “Upload” button. I would like the text in the upload button to display “Upload x files” where x is the number of items in the list.
I am using a converter which takes the ItemsSource property as input and returns the string, however if I add or remove items from the listview (ie. it’s underlying collection) the converter on the button binding is not called.
What am I doing incorrectly?
I have the base class:
public class FileItem : INotifyPropertyChanged { ... }
And an ObservableCollection:
public class Files : ObservableCollection<FileItem> {}
An I assign the collection to the ListView.
lvw_FileList.ItemsSource = new Files();
The ListView has it’s ItemSource property bound.
<ListView x:Name="lvw_FileList" ItemsSource="{Binding Mode=OneWay}">
...
</ListView>
The button which text content needs a converter based on the ListView.ItemsSource.
<Button Content="{Binding ElementName=lvw_FileList, Path=ItemsSource, Converter={StaticResource UpdateButtonConverter}}" x:Name="btn_Upload" />
Bindings only listen to
PropertyChangedand notCollectionChangednotifications. If you need to run the conversion whenever the collection changes consider aMultiBindingto both the collection and itsCountproperty. The count change will trigger a reevaluation and the collection value can be used in the converter.