绑定到ICollectionView不会显示任何内容

时间:2019-05-20 08:07:13

标签: c# wpf

当我直接绑定到EventsSource时它起作用,但是当我更改为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();
}

此代码可能有什么问题?

我不想使用默认视图( WPF CollectionViewSource Multiple Views?

1 个答案:

答案 0 :(得分:0)

我将其设置为依赖项属性,并且可以正常工作。不确定这是否是解决问题的最佳方法。

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);
}