Hou*_*man 6 .net c# silverlight wpf collectionview
当<Label Content="{Binding ItemCount}"/>我的 View 上有一个绑定到 ViewModel 上的属性时。
在视图模型上,我将属性定义为
public int ItemCount
{
get { RowViewModelsCollectionView.Count; }
}
Run Code Online (Sandbox Code Playgroud)
我清楚地要求对 进行计数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;
}
Run Code Online (Sandbox Code Playgroud)
我应该如何解决这个问题?
@punker76,说绑定应该Count直接对集合视图的属性进行是正确的......
原因是它CollectionView已经实现INotifyPropertyChanged并通知其Count属性的属性更改,只要在其上发生提交、过滤、分组、排序......
因此,假设您拥有RowViewModelsCollectionView视图模型的公共/内部属性,
<Label Content="{Binding RowViewModelsCollectionView.Count}"/>
Run Code Online (Sandbox Code Playgroud)
.... 应该可以正常工作...
你为什么不用这个?
<Label Content="{Binding ModelView.RowViewModelsCollectionView.Count}"/>
Run Code Online (Sandbox Code Playgroud)
这是一个小例子。
<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));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11105 次 |
| 最近记录: |