11

it seems that I have a problem with my multibinding.

Scenario:
I have a window with two datepickers and a listview. The listliew contains some data bound elements called "entries". An entry has a property called "date".

I just want my listview to show entries whose date is in between my two datepickes dates.

My xaml code for binding the listview to the entries and dates:

<ListView.ItemsSource>
  <MultiBinding Converter="{StaticResource EntriesFilterConv}"
                UpdateSourceTrigger="PropertyChanged">
    <Binding Path="Entries" UpdateSourceTrigger="PropertyChanged"/>
    <Binding ElementName="EntryFromDate" Path="SelectedDate"
             UpdateSourceTrigger="PropertyChanged"/>
    <Binding ElementName="EntryToDate" Path="SelectedDate"
             UpdateSourceTrigger="PropertyChanged"/>
  </MultiBinding>
</ListView.ItemsSource>

However, this doesnt work. My converter is called when a SelectedDate changes but its never called when Entries changes.

With normal data binding like this:

<ListView ItemsSource="{Binding Entries}">
  ...
</ListView>

The listview updates normally. Any idea?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Michael Hilus
  • 1,647
  • 2
  • 21
  • 42

2 Answers2

13

After searching for hours, I find a simple and decent answer ! Since ObservableCollection doesn't raise PropertyChanged event but CollectionChanged, we just have to bind the collection's Count to fire the event when the list changes :

<MultiBinding Converter="{Resources:ListToStringConverter}">
    <Binding Path="List.Count" />
    <Binding Path="List" />
</MultiBinding>

Original infos about this perfectly working multibinding here : https://stackoverflow.com/a/10884002/817504

Community
  • 1
  • 1
Profet
  • 944
  • 1
  • 15
  • 22
  • 5
    That is not recommended because it may cause a memory leak as you can see here: [Link](https://support.microsoft.com/en-us/kb/938416). It happens because that property is not a dependency property and doesn't implement INotifyPropertyChanged, so the only way WPF has to "bind" to that property is using PropertyDescriptor, that attaches a PropertyChanged event causing a hard reference to that property. – Carlos Teixeira Aug 10 '16 at 11:16
  • 1
    If you have your property in a viewmodel, you can raise the property changed event manually when the collection changes (in the setter, for example). – Carlos Teixeira Aug 25 '16 at 06:33
6

I think the following might cause this: If you bind directly to the Entries the ListView will listen to CollectionChanged events, but if such a binding is inside a MultiBinding the only thing that would cause a reevaluation could be a PropertyChanged notification, which might not exist for the Entries property in your model.

Maybe you can subscribe to the CollectionChanged event of your collection and raise a PropertyChanged event or get the BindingExpression within your MultiBinding to call an update manually.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I will try so. But what property should I use? Is there a property called `this`? – Michael Hilus Apr 07 '11 at 16:34
  • What does your model look like? Where is `Entries` defined? Also take a look at the INotifyPropertyChanged documentation if you are not familiar with that: http://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged.aspx – H.B. Apr 07 '11 at 16:39
  • I have a generic class `ViewModelCollection`which inherits from `IList`, `INotifyCollectionChanged` and `INotifyPropertyChanged`. In this case `Entries`is a `ViewModelCollection`. Can I subscribe to the `CollectionChanged` within my class? – Michael Hilus Apr 07 '11 at 16:45
  • Oh, maybe you could try raising PropertyChanged with `Binding.IndexerName` then. – H.B. Apr 07 '11 at 17:21