2011-05-10 75 views
0

我想DataPager的绑定到数据网格问题Silverlight 4的DataPager控件

这里是XAML

<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> 

和视图模型

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 the resultenter image description here

正如你所看到的DataPager不工作 有什么问题?

+0

您正在设置MainPage的新实例的ItemsSource - 是否打算? – thomasmartinsen 2011-05-10 10:30:50

+0

是 这是ViewModel而不是MainPage.xaml的codeBihande,只是试图访问位于MainPage.zaml文件中的datagrids对象 – user746499 2011-05-10 12:15:54

回答

1

尝试将您的PagedCollectionView作为ViewModel上的属性公开,并将DataGrid和Pager上的ItemsSource绑定到PagedCollectionView上。

+0

是的,我这样做,它现在有效,但还有另一个问题 当我点击网格时,它不是影响。它只是冻结。 当我尝试将数据页绑定到数据网格时,它并不是这样早期的 – user746499 2011-05-10 14:24:40