6

<Label Content="{Binding ItemCount}"/>ViewModel のプロパティにバインドする Viewがある場合。

ビューモデルでは、次のように定義されたプロパティがあります

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

Count@ punker76は、コレクションビューのプロパティに直接バインドを行う必要があると言っているのは正しいです...

理由は、コミット、フィルタリング、グループ化、並べ替えが行われるたびに、そのプロパティのプロパティ変更をCollectionView実装して通知するためです...INotifyPropertyChangedCount

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 に答える