6

当我的<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;
        }

我应该如何解决这个问题?

4

2 回答 2

5

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

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

因此,假设您拥有RowViewModelsCollectionView视图模型的公共/内部属性,

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

....应该工作得很好...

于 2011-11-21T14:44:32.583 回答
2

你为什么不使用这个?

<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));
      }
    }
  }
}
于 2011-11-21T11:24:15.633 回答