直接用DataGrid.ItemSource = DataTable.DefaultView时会出现以下错误:
target element is 'TextBlock' (Name=''); target property is 'Text
需要用CollectionViewSource来做中间转换 .另外实时更新数据,所以不能把它做成StaticResource。
要想DataGrid render速度快,需要支持虚拟化,及设置列宽和行高。
private void BtnFirstPage_Click(object sender, RoutedEventArgs e) { this.mData.DataPageNum = 1; this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize); } private void BtnPrevPage_Click(object sender, RoutedEventArgs e) { this.mData.DataPageNum--; this.mData.DataPageNum = this.mData.DataPageNum > 1 ? this.mData.DataPageNum : 1; this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize); } private void BtnNextPage_Click(object sender, RoutedEventArgs e) { int maxPageNum = (int)Math.Ceiling((double)this.mData.Times.Count / (double)this.mData.DataPageSize); this.mData.DataPageNum++; this.mData.DataPageNum = this.mData.DataPageNum > maxPageNum ? maxPageNum : this.mData.DataPageNum; this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize); } private void BtnLastPage_Click(object sender, RoutedEventArgs e) { int maxPageNum = (int)Math.Ceiling((double)this.mData.Times.Count / (double)this.mData.DataPageSize); this.mData.DataPageNum = maxPageNum; this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize); } private void SetDisData(int pageNum, int pageSize) { DataRow row; int startRow = (pageNum - 1) * pageSize; int endRow = startRow + pageSize; endRow = this.mData.Times.Count > endRow ? endRow : this.mData.Times.Count; this.mDtData.Rows.Clear(); CollectionViewSource itemCollectionViewSource2 = new CollectionViewSource(); itemCollectionViewSource2.Source = this.mDtData; DgdData.DataContext = itemCollectionViewSource2; }