Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定到ICollectionView不会';我什么也看不出来_C#_Wpf - Fatal编程技术网

C# 绑定到ICollectionView不会';我什么也看不出来

C# 绑定到ICollectionView不会';我什么也看不出来,c#,wpf,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.

当我直接绑定到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();
}
//EventsControl类
专用布尔过滤器(对象obj)
{
如果(!(obj为Event@Event))返回false;
if(string.IsNullOrEmpty(Location))返回true;
返回true;
//return@event.Location==位置;
}
公共静态只读DependencyProperty事件SourceProperty=DependencyProperty.Register(
名称(EventsSource)、类型(ObservableCollection)、类型(EventsControl)、新属性元数据(默认值(ObservableCollection)、EventsSourceChanged);
公共可观察收集事件来源
{
get=>(ObservableCollection)GetValue(EventsSourceProperty);
set=>SetValue(EventsSourceProperty,value);
}
公共ICollectionView事件视图{get;set;}
私有静态无效事件源更改(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
如果(!(d为EventsControl EventsControl))返回;
var view=newcollectionviewsource{Source=e.NewValue}.view;
view.Filter+=eventsControl.Filter;
eventsControl.EventsView=视图;
//view.Refresh();
}
此代码可能有什么问题

我不想使用默认视图(
)

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

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

eventSource属性正在通知订阅者-我不确定其他属性是否会发生这种情况。它要么实现[INotifyPropertyChanged]()接口,要么返回
PropertyChanged
事件。