如何获得一个CollectionView的真实计数,当过滤器是在使用

本文关键字:过滤器 真实 何获得 CollectionView 一个 | 更新日期: 2023-09-27 18:15:14

当我有一个<Label Content="{Binding ItemCount}"/>在我的视图绑定到ViewModel的属性。

在视图模型上,我将属性定义为

public int ItemCount
{
    get { RowViewModelsCollectionView.Count; }
}

我清楚地要求计数CollectionView,在那里我期望得到仅可见项目的计数。不幸的是,我得到了整个行的计数,甚至是那些由于过滤器而没有显示在视图上的行。

更新:

在男星

:

RowViewModelsCollectionView= new ListCollectionView(rowViewModels) {Filter = Contains};

private bool Contains(object obj)
        {
            RowViewModel rowViewModel = obj as RowViewModel;
            if (rowViewModel != null && Books.ContainsKey(rowViewModel.Book))
            {
                RaisePropertyChanged("ItemCount"); // Trying out to raise it without joy
                return true;
            }
            return false;
        }

我该如何解决这个问题?

如何获得一个CollectionView的真实计数,当过滤器是在使用

@punker76,说绑定应该直接做到集合视图的Count属性是正确的…

原因是CollectionView已经实现了INotifyPropertyChanged,并且每当在其上发生提交,过滤,分组,排序时,都会通知其Count属性的属性更改…

假设RowViewModelsCollectionView是视图模型的公共/内部属性,

  <Label Content="{Binding RowViewModelsCollectionView.Count}"/> 

你为什么不用这个呢?

<Label Content="{Binding ModelView.RowViewModelsCollectionView.Count}"/>

这是一个小例子。

<Window x:Class="WPFValidation.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window"
        Height="300"
        Width="300">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
      <RowDefinition />
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0"
             Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}" />
    <TextBlock Grid.Row="1"
               Text="{Binding ModelListView.Count}" />
    <ListBox Grid.Row="2"
             ItemsSource="{Binding ModelListView}" />
  </Grid>
</Window>
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
namespace WPFValidation
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow() {
      this.DataContext = new ModelView();
      this.InitializeComponent();
    }
  }
  public class ModelView : INotifyPropertyChanged
  {
    public event PropertyChangedEventHandler PropertyChanged;
    private ICollectionView modelListView;
    private ICollection<string> collection;
    public ModelView() {
      this.collection = new ObservableCollection<string>(new[] {"test1", "test2", "filtering"});
    }
    public ICollectionView ModelListView {
      get { return this.modelListView ?? this.GetModelListView(); }
    }
    private ICollectionView GetModelListView() {
      var collectionView = CollectionViewSource.GetDefaultView(this.collection);
      collectionView.Filter += o => o == null || string.IsNullOrEmpty(this.FilterText) || o.Equals(this.FilterText);
      return collectionView;
    }
    private string filterText;
    public string FilterText {
      get { return this.filterText; }
      set {
        if (value != this.filterText) {
          this.filterText = value;
          this.ModelListView.Refresh();
          this.RaisePropertyChange("FilterText");
        }
      }
    }
    private void RaisePropertyChange(string propertyName) {
      var eh = this.PropertyChanged;
      if (eh != null) {
        eh(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }
}