为什么DataPager没有拿起我的TotalItemCount?

时间:2011-01-28 15:53:19

标签: silverlight mvvm datapager

我正在尝试创建一个MVVM友好页面,其中包含Silverlight的数据网格和数据寻呼机。在我的viewmodel上,我实现了IPagedCollectionView接口

public class DiscountViewModel : INotifyPropertyChanged, IPagedCollectionView

我已经实现了获取ItemCount和TotalItemCount所需的所有方法。

public bool CanChangePage {
        get { return TotalItemCount > PageIndex * PageSize; }
    }

    public bool IsPageChanging {
        get { return false; }
    }

    public int ItemCount {
        get { return itemCount; }
        set { itemCount = value; RaisePropertyChange("ItemCount"); }
    }

   public bool MoveToFirstPage() {
        PageChanging(this, new PageChangingEventArgs(PageIndex));
        PageIndex = 0;
        PageChanged(this, null);
        return true;
    }

    public bool MoveToLastPage() {
        throw new NotImplementedException();
    }

    public bool MoveToNextPage() {
        PageChanging(this, new PageChangingEventArgs(PageIndex));
        PageIndex++;
        PageChanged(this, null);
        return true;
    }

    public bool MoveToPage(int pageIndex) {
        PageChanging(this, new PageChangingEventArgs(PageIndex));
        PageIndex = pageIndex;
        PageChanged(this, null);
        return true;
    }

    public bool MoveToPreviousPage() {
        PageChanging(this, new PageChangingEventArgs(PageIndex));
        PageIndex--;
        PageChanged(this, null);
        return true;
    }

    public event EventHandler<EventArgs> PageChanged;

    public event EventHandler<PageChangingEventArgs> PageChanging;

    public int PageIndex {
        get { return pageIndex; }
        set { pageIndex = value; RaisePropertyChange("PageIndex"); }
    }

    public int PageSize {
        get { return pageSize; }
        set { pageSize = value; RaisePropertyChange("PageSize"); }
    }

    public int TotalItemCount {
        get { return totalItemCount; }
        set { totalItemCount = value; RaisePropertyChange("TotalItemCount"); }
    }

XAML可以正常绑定到项目,数据网格显示最初加载时的前五个项目。

<data:DataGrid x:Name="discountsDataGrid"  ItemsSource="{Binding Discounts, Mode=TwoWay}"  MinHeight="200" AutoGenerateColumns="False" SelectedItem="{Binding SelectedDiscount, Mode=TwoWay}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="RowEditEnded">
                            <i:InvokeCommandAction Command="{Binding SaveChangesCommand}" CommandParameter="{Binding SelectedDiscount}"  />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>

                    <data:DataGrid.Columns>
                        <data:DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                        <data:DataGridTextColumn Header="Discount Amount" Binding="{Binding Amount}" />
                    </data:DataGrid.Columns>
                </data:DataGrid>
                <sdk:DataPager  PageSize="{Binding PageSize}"  Source="{Binding Path=ItemsSource, ElementName=discountsDataGrid}" Margin="0,-1,0,0" />

获得折扣然后设置总计数的相关代码全部执行,数字出现在我期望的范围内(退回的项目为5,所有折扣的总数为9)

public void LoadDiscounts(Object parameter){
        EntityQuery<Discount> eq = ctx.GetPagedDiscountsQuery(PageIndex, PageSize, "");
        eq.IncludeTotalCount = true;
        ctx.Load<Discount>(eq, OnDiscountsLoaded, null);
    }

    private void OnDiscountsLoaded(LoadOperation<Discount> loadOperation) {
        if (loadOperation.Error != null) {

        } else {
            Discounts = loadOperation.Entities;   
            ItemCount = loadOperation.TotalEntityCount;
            ctx.GetDiscountCount(OnCountCompleted, null);
            RaisePropertyChange("Discounts");
        }
    }

    private void OnCountCompleted(InvokeOperation<int> op) {
        TotalItemCount = op.Value;
        RaisePropertyChange("Discounts");
        RaisePropertyChange("TotalItemCount");
    }

但是数据目录器似乎没有提到超过5个折扣。我可以看到TotalItemCount已正确设置。关于这个奇怪的是,除了设置值之外,任何其他代码都不会调用TotalItemCount属性。数据采集​​器是否应该使用它来确定是否可以单击上一个/下一个/第一个/最后一个按钮?

更新 所以我在订阅MouseEnter事件时查看了DataPager,发现了一些有趣的东西。 ItemCount为5,无论我在ViewModel上设置它(例如,手动将其设置为9),DataPager只查看其集合中的项目以确定实际有多少项目。它不是从ViewModel类中读取这些值。我以为我读过如果周围的上下文实现IPagedCollectionView,那么DataPager将使用这些方法来确定大小/页面/等。

2 个答案:

答案 0 :(得分:1)

DataPager控件Source属性应绑定到集合。不是DataGrid控件的ItemsSource属性。

来自DataPager.Source property

  

源可以是任何IEnumerable   采集。当绑定到   IEnumerable没有实现   IPagedCollectionView,DataPager   表现得好像所有数据都在a上   单页。设置DataPager   属性不会有任何影响   控制。

     

通常,源是一个集合   实现IPagedCollectionView。   IPagedCollectionView提供   分页功能,以及   DataPager控件为用户提供   与...交互的界面   IPagedCollectionView。提供   分页功能   IEnumerable集合,你可以包装   它在PagedCollectionView类中。

List<String> itemList = new List<String>();
// Generate some items to add to the list.
for (int i = 1; i <= 33; i++)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder("Item ");
    sb.Append(i.ToString());
    itemList.Add(sb.ToString());
}
// Wrap the itemList in a PagedCollectionView for paging functionality
PagedCollectionView itemListView = new PagedCollectionView(itemList);

// Set the DataPager and ListBox to the same data source.
dataPager1.Source = itemListView;
listBox1.ItemsSource = itemListView;

答案 1 :(得分:0)

我注意到您正在使用WCF RIA服务。在更简单的场景中,您只需将DataGrid和DataPager绑定到DomainDataSource,就不必实现任何操作。但我猜你有更复杂的要求,这就是为什么你手工做的一切。