Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I want to bind DataPager to DataGrid

here is xaml

<<pre lang="xml">UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="NorthWindSilver.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:mv="clr-namespace:NorthWindSilver.ViewModel"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <mv:ViewModel x:Key="ViewModel"/>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid Name="dgCustomer" AutoGenerateColumns="True" ItemsSource="{Binding Items, Mode=TwoWay, Source={StaticResource ViewModel}}">
        </data:DataGrid>
        <sdk:DataPager HorizontalContentAlignment="Center" x:Name="myPager" Grid.Row="2" Source="{Binding Path=ItemsSource, ElementName=dgCustomer}" PageSize="10"/>
    </Grid>
</UserControl

>

and ViewModel

public class ViewModel :  INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<Customer> _items;

    public ViewModel()
    {
        if (!IsDesignTime)
            this.Customer();
    }

    public void ChangeProperty(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }      

    public ObservableCollection<Customer> Items
    {
        get
        {
            return this._items;
        }
        set
        {
            this._items = value;
            ChangeProperty("Items");
        }
    }


    public bool IsDesignTime
    {
        get
        {
            return (Application.Current == null) ||
                (Application.Current.GetType() == typeof(Application));
        }
    }


    public void Customer()
    {
        DataServiceClient webService = new DataServiceClient();
        webService.GetCustomersCompleted += new EventHandler<GetCustomersCompletedEventArgs>(webService_GetCustomersCompleted);

        webService.GetCustomersAsync();
    }


    void webService_GetCustomersCompleted(object sender, GetCustomersCompletedEventArgs e)
    {
        Items = e.Result;

        PagedCollectionView pageView = new PagedCollectionView(Items);          

        MainPage ma = new MainPage();

        ma.dgCustomer.ItemsSource = pageView;
    }
}




Here is result

As you see DataPager does not work
what the problem?
Posted

1 solution

Modify your code to

1. Create a property
<pre lang="css">private PagedCollectionView _view;
        public PagedCollectionView PagedView
        {
            get
            {
                return this._view;
            }
            set
            {
                this._view = value;
                ChangeProperty("PagedView");
            }
        }




2. Set this Property in webService_GetCustomersCompleted handler

PagedCollectionView pageView = new PagedCollectionView(((ViewModel)dgCustomer.DataContext).Items);
            pageView.PageSize = 10;
            PagedView = pageView;


3. Bind your grid using this property

DataContext="{StaticResource ViewModel}" ItemsSource="{Binding Path=PagedView, Mode=TwoWay}"
 
Share this answer
 
Comments
Sider89 10-May-11 8:12am    
well I did it as you posted but it gives exception

see that picture Link
Sider89 10-May-11 8:41am    
@Manish Ranjan Kumar - thank you very much It works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900