是否有任何方法可以自动更新过滤器,ICollectionView而无需Refresh()在进行相关更改时进行调用?
我有以下内容:
[Notify]
public ICollectionView Workers { get; set; }
此属性中的[Notify]属性刚刚实现,INotifyPropertyChanged但在这种情况下它似乎没有做任何事情.
Workers = new CollectionViewSource { Source = DataManager.Data.Workers }.View;
Workers.Filter = w =>
    {
        Worker worker = w as Worker;
        if (w == null)
            return false;
        return worker.Employer == this;
    };
在XAML中:
<TextBlock x:Name="WorkersTextBlock"
           DataContext="{Binding PlayerGuild}"
           FontFamily="Pericles"
           Text="{Binding Workers.Count,
                          StringFormat=Workers : {0},
                          FallbackValue=Workers : 99}" />
更新:看起来使用ICollectionView对我来说是必要的,所以我想重新审视这个主题.我正在为这个问题添加一笔赏金,其收件人将是任何能够提供有关如何实现ICollectionView不需要手动刷新的"不干涉"的洞察力的人.在这一点上,我对任何想法持开放态度.
我有以下绑定到 DataGrid 的 ObservableCollection:
public ObservableCollection<Message> Messages = new ObservableCollection<Message>;
XAML:
<DataGrid ItemsSource="{Binding Path=Messages}">
我在启动时对其进行排序,使用默认视图:
ICollectionView view = CollectionViewSource.GetDefaultView(Messages);
view.SortDescriptions.Add(new SortDescription("TimeSent", ListSortDirection.Descending));
一切正常,但问题是每当我向 Messages 集合添加新消息时,它只会附加到列表的底部,而不是自动排序。
Messages.Add(message);
难道我做错了什么?我确信我可以通过每次添加项目时刷新视图来解决这个问题,但这似乎是错误的做法(更不用说性能方面了)。