2016-01-16 92 views
0

我想創建可以分頁的通用Datagrid。當我打電話給我的DataGrid_UC的構造函數並通過20 Employees時,它確實存儲了AllObject Observable Collection中的所有20名員工。並且將CurrentPageItems濾除爲5.但是,在設置datagrid.ItemsSource = CurrentPageItems時,其throws null exception但CurrentPageItems確實包含5個項目。Paggeable Datagrid ItemSource拋出null異常

DataGrid_UC.xaml

public partial class DataGrid_UC : UserControl 
    { 
     private ObservableCollection<object> _currentPageItems; 
     public ObservableCollection<object> CurrentPageItems 
     { 
      get 
      { 
       return _currentPageItems; 
      } 
      private set 
      { 
       if (_currentPageItems != value) 
       { 
        _currentPageItems = value; 
       } 
      } 
     } 

     // Default Entries per page Number 
     private int _pageSize = 5; 
     public int PageSize 
     { 
      get 
      { 
       return _pageSize; 
      } 
      set 
      { 
       if (_pageSize != value) 
       { 
        _pageSize = value; 
        Reset(); 
       } 
      } 
     } 

     public int TotalPagesNumber 
     { 
      get 
      { 
       if (AllObjects != null && PageSize > 0) 
       { 
        return (AllObjects.Count - 1)/PageSize + 1; 
       } 
       return 0; 
      } 
     } 

     private int _currentPageNumber = 1; 
     public int CurrentPageNumber 
     { 
      get 
      { 
       return _currentPageNumber; 
      } 

      protected set 
      { 
       if (_currentPageNumber != value) 
       { 
        _currentPageNumber = value; 

       } 
      } 
     } 

     protected ObservableCollection<object> AllObjects { get; set; } 

     public DataGrid_UC() 
     { 
      InitializeComponent(); 
      dataGrid.ItemsSource = CurrentPageItems; 
     } 

     public DataGrid_UC(IEnumerable<object> allObjects, int? entriesPerPage = null) 
     { 
      AllObjects = new ObservableCollection<object>(allObjects); 

      if (entriesPerPage != null) 
       PageSize = (int)entriesPerPage; 

      SetCurrentPageItems(); 
     } 

     #region Public Methods 

     public void GoToNextPage() 
     { 
      if (CurrentPageNumber != TotalPagesNumber) 
      { 
       CurrentPageNumber++; 
       SetCurrentPageItems(); 
      } 
     } 

     public void GoToPreviousPage() 
     { 
      if (CurrentPageNumber > 1) 
      { 
       CurrentPageNumber--; 
       SetCurrentPageItems(); 
      } 
     } 





     #endregion 

     #region Private Methods 

     public void SetCurrentPageItems() 
     { 
      int upperLimit = CurrentPageNumber * PageSize; 

      CurrentPageItems = 
       new ObservableCollection<object>(
        AllObjects.Where(x => AllObjects.IndexOf(x) > upperLimit - (PageSize + 1) && AllObjects.IndexOf(x) < upperLimit)); 

      dataGrid.ItemsSource = CurrentPageItems; 
     } 

     private void Reset() 
     { 
      CurrentPageNumber = 1; 
      SetCurrentPageItems(); 
     } 

     #endregion 

     private void next_Click(object sender, RoutedEventArgs e) 
     { 
      if (CurrentPageNumber != TotalPagesNumber) 
      { 
       CurrentPageNumber++; 
       SetCurrentPageItems(); 
      } 
     } 

     private void previous_Click(object sender, RoutedEventArgs e) 
     { 
      if (CurrentPageNumber > 1) 
      { 
       CurrentPageNumber--; 
       SetCurrentPageItems(); 
      } 
     } 



    } 

主要Window.xaml

public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      List<Employee> emp = new List<Employee>(); 
      for (int i = 0; i < 20; i++) 
      { 
       Employee e = new Employee(); 
       e.ID = i; 
       e.Name = "Test " + i; 
       emp.Add(e); 
      } 

      DataGrid_UC d = new DataGrid_UC(emp, 5); 
      newContentControl.Content = d; 
     } 
    } 

回答

1

dataGrid成員爲null,因爲你的第二個構造缺少InitializeComponent調用,它(除其他事項外)初始化在XAML中定義的類成員x:Name

所以改變這樣的構造:

public DataGrid_UC(IEnumerable<object> allObjects, int? entriesPerPage = null) 
{ 
    InitializeComponent(); 
    AllObjects = new ObservableCollection<object>(allObjects); 
    ... 
}