Silverlight DataPager的页数不随查询结果集(数量)的变化而变化的有关问题

Silverlight DataPager的页数不随查询结果集(数量)的变化而变化的问题
本帖最后由 wpf3d 于 2015-10-01 05:51:19 编辑
电子书“Pro Business Applications with Silveright 5"第228页,有个用WCF RIA Services Toolkit中的DomainCollectionView的例子。基本上照着例子敲的代码。
就是一个TextBox和DataGrid,在TextBox里输入Product Name时,实时地把符合条件的记录显示在DataGrid上。
问题是如图,符合条件的记录只剩两条,可DataPager还是6页。

Silverlight DataPager的页数不随查询结果集(数量)的变化而变化的有关问题

DataGrid与DataPager都绑定的同一个CollectionView.

 <TextBox Grid.Column="1" x:Name="ProductNameTextBox" Width="150" HorizontalAlignment="Left" Text="{Binding ProductNameFilter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    
 <sdk:DataGrid Height="254" ItemsSource="{Binding ProductsCollectionView} "  AutoGenerateColumns="False">
    <sdk:DataGrid.Columns>
         <sdk:DataGridTextColumn Header="Product Id" SortMemberPath="ProductId" Binding="{Binding ProductId}" />
        <sdk:DataGridTextColumn Header="Product Name" SortMemberPath="ProductName" Binding="{Binding ProductName}" />
     </sdk:DataGrid.Columns>
</sdk:DataGrid>

<sdk:DataPager Source="{Binding ProductsCollectionView}" />



public class HomeViewModel
{
    private NorthwindDomainContext _context = new NorthwindDomainContext();

    private DomainCollectionView<Product> _view;
    private DomainCollectionViewLoader<Product> _loader;
    private EntityList<Product> _source;

    public HomeViewModel()
    {
        _loader = new DomainCollectionViewLoader<Product>(LoadProducts, LoadProductsCompleted);
        _source = new EntityList<Product>(_context.Products);
        _view = new DomainCollectionView<Product>(_loader, _source);

        INotifyCollectionChanged notifySortDescriptions = (INotifyCollectionChanged)_view.SortDescriptions;
        notifySortDescriptions.CollectionChanged += (s, e) => this._view.MoveToFirstPage();

        using (this._view.DeferRefresh())
        {
            this._view.PageSize = 15;
            this._view.MoveToFirstPage();

        }
    }
    LoadOperation<Product> LoadProducts()
    {
        EntityQuery<Product> query = _context.GetProductsQuery();

        if (!string.IsNullOrEmpty(ProductNameFilter))
        {
            QueryBuilder<Product> builder = new QueryBuilder<Product>();
            query = builder.ApplyTo(query.Where(p => p.ProductName.Contains(ProductNameFilter)));
        }
        query.IncludeTotalCount = true; // 设置了这一行也还是不行。
        return _context.Load(query.SortAndPageBy(this._view));
    }
    void LoadProductsCompleted(LoadOperation<Product> lo)
    {
        if (lo.HasError)
        {
            lo.MarkErrorAsHandled();
            MessageBox.Show(lo.Error.Message);
        }
        else if (!lo.IsCanceled)
        {
            this._source.Source = lo.Entities;

            if (lo.TotalEntityCount != -1)
            {
                this._view.SetTotalItemCount(lo.TotalEntityCount);
            }
        }
    }
    public ICollectionView ProductsCollectionView
    {
        get { return this._view; }
    }

    private string productNameFilter;
    public string ProductNameFilter
    {
        get { return productNameFilter; }
        set
        {
            productNameFilter = value;
            this._view.Refresh();
        }
    }
}


请大伙儿帮帮忙,谢谢。
------解决思路----------------------
引用:
结论:

private string productNameFilter;
public string ProductNameFilter
{
    get { return productNameFilter; }
    set
    {
        productNameFilter = value;
        OnSearch();
    }
}
private void OnSearch()
{
    // This makes sure we refresh even if we're already on the first page
    using (ProductsCollectionView.DeferRefresh())
    {
        // This will lead us to re-query for the total count
        ProductsCollectionView.SetTotalItemCount(-1);
        ProductsCollectionView.MoveToFirstPage();
    }
}

但其中的逻辑不是很清晰。

说不上来。感觉是  先清空标示,后赋值。