Paggeable Datagrid ItemSource抛出null异常

时间:2016-01-16 22:04:55

标签: c# wpf xaml datagrid pagination

我想创建一个可以分页的通用Datagrid。当我调用DataGrid_UC的构造函数并传递20 Employees时,它会将所有20个Employees存储在AllObject Observable Collection中。并将CurrentPageItems过滤为5.但在设置datagrid.ItemsSource = CurrentPageItemsthrows null exception,但CurrentPageItems确实包含5个项目。

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();
            }
        }



    }

主窗口.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 个答案:

答案 0 :(得分:1)

dataGrid成员为null,因为您的第二个构造函数缺少InitializeComponent调用,其中(除其他外)通过x:Name初始化XAML中定义的类成员。

所以改变构造函数如下:

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