views:

370

answers:

1

In a Silverlight project I have a view with a list (DataGrid) of cases. The list is paged with DataPager. My source collection is wrapped in an PagedCollectionView.

When an item is created it is added to the list and set as selecteditem in the DataGrid, depending on the list sorting, this could be on another page the the current active in the datapager.

How would you go about moving the datapager to the page of newly added item?

    public PagedCollectionView<CaseDTO> Cases { get; set; }

    public void CreateCase()
    {
        var requestDispatcher = container.GetInstance<IAsyncRequestDispatcher>();
        requestDispatcher.Add(GetRequest<CreateCaseRequest>());
        requestDispatcher.Add(GetRequest<GetCasesRequest>());

        requestDispatcher.ProcessRequests(
            responses =>
            {
                selectedCaseId = responses.Get<CreateCaseResponse>().CaseId;
                UpdateCases(responses.Get<GetCasesResponse>());

                Cases.MoveToPageOf(SelectedCase); // How to implement?
            },
            ex => { throw new Exception(ex.ToString()); }
            );
    }

Cases.MoveToPageOf(SelectedCase); // How to implement?

A: 

I found the best way was just to search for the page. I made the following extension method that does just that:

public static class PagedCollectionViewExtension
{
    public static bool MoveToPageOf(this PagedCollectionView collectionView, object item)
    {
        int pageSearched = 0;

        while (pageSearched < collectionView.PageSize)
        {
            if (collectionView.IndexOf(item) != -1)
                return true;

            collectionView.MoveToNextPage();
            pageSearched++;
        }

        return false;
    }
}
Lars Udengaard