Sam Debruyn
Sam Debruyn

Reputation: 938

Update XAML binding on CollectionChanged with ObservableCollection

I am working on a Xamarin (Android, iOS, WP 8.1 RT) app with MVVM Light. My ViewModel has 2 ObservableCollections so my view on WP also contains 2 ListViews.

Let's say OC1 is bound to LV1 and OC2 is bound to LV2.

The first OC is never empty. The second OC is almost always empty.

I'd like to do the following:

I tried binding the Visibility properties of both ListViews to OC2 using a converter that returns Visibility.Collapsed or Visibility.Visible depending on the amount of items in the given collection.

That initially works, but the visibility isn't updated when items are added or removed from OC2. It only works when the setter of OC2 is used (which is only at initialization).

This question is not a duplicate of this one as I'd prefer to only use XAML-bindings or small modifications to my ViewModel with as less code-behind as possible.

Upvotes: 1

Views: 1055

Answers (1)

Sam Debruyn
Sam Debruyn

Reputation: 938

Thanks @3615, that fixed it!

In the ViewModel:

OC2.CollectionChanged += (sender, args) => RaisePropertyChanged(() => OC2);

Upvotes: 3

Related Questions