-1

It works when I bind directly to EventsSource but it doesn't when I change the binding to EventsView.

// EventsControl class

private bool Filter(object obj)
{
    if (!(obj is Event @event)) return false;
    if (string.IsNullOrEmpty(Location)) return true;

    return true;
    //  return @event.Location == Location;
}

public static readonly DependencyProperty EventsSourceProperty = DependencyProperty.Register(
    nameof(EventsSource), typeof(ObservableCollection<Event>), typeof(EventsControl), new PropertyMetadata(default(ObservableCollection<Event>), EventsSourceChanged));

public ObservableCollection<Event> EventsSource
{
    get => (ObservableCollection<Event>)GetValue(EventsSourceProperty);
    set => SetValue(EventsSourceProperty, value);
}

public ICollectionView EventsView { get; set; } 

private static void EventsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    if (!(d is EventsControl eventsControl)) return;

    var view = new CollectionViewSource { Source = e.NewValue }.View;
    view.Filter += eventsControl.Filter;
    eventsControl.EventsView = view;
    //view.Refresh();
}

What could be wrong with this code?

I don't want to use a default view ( WPF CollectionViewSource Multiple Views? )

Konrad
  • 6,385
  • 12
  • 53
  • 96
  • 1
    the eventSource property is notifying subscribers - i'm not sure if that is happening for you with the other property – jazb May 20 '19 at 08:11

1 Answers1

0

I made it a dependency property and it works. Not sure if that's the best way to solve it though.

public static readonly DependencyProperty EventsViewProperty = DependencyProperty.Register(
            nameof(EventsView), typeof(ICollectionView), typeof(EventsControl), new PropertyMetadata(default(ICollectionView)));

public ICollectionView EventsView
{
    get => (ICollectionView) GetValue(EventsViewProperty);
    set => SetValue(EventsViewProperty, value);
}
Konrad
  • 6,385
  • 12
  • 53
  • 96
  • 1
    It's either that or implement the [INotifyPropertyChanged ](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=netframework-4.8) interface and rairse the `PropertyChanged` event. – mm8 May 20 '19 at 13:21