2009-11-06 112 views
0

我正在將我的應用程序從Windows窗體移植到WPF。我發現datagrid只能通過WPF Toolkit獲得。不過,我有一些性能問題。這可能是因爲在Windows窗體中執行正常的東西不能在wpf中執行,因爲它必須以不同的方式實現。總之,我這樣做是這樣的:wpf工具包datagrid性能

// XAML 
<toolkit:DataGrid x:Name="dataGrid" ItemsSource="{Binding Path=.}"/> 


// C# 
DataTable dataTable = new DataTable(); 
MyViewModel viewModel = new MyViewModel(); 

this.dataGrid.AutoGenerateColumns = false; 
this.dataGrid.CanUserAddRows = false; 
this.dataGrid.CanUserDeleteRows = false; 
this.dataGrid.CanUserReorderColumns = true; 
this.dataGrid.CanUserResizeColumns = true; 
this.dataGrid.CanUserResizeRows = false; 
this.dataGrid.CanUserSortColumns = true; 
this.dataGrid.IsReadOnly = true; 
this.dataGrid.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; 
this.dataGrid.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; 

    this.dataGrid.ColumnHeaderHeight = 18; 
    this.dataGrid.RowHeight = 18; 
    this.dataGrid.DataContext = vm.dataGrid; 

    List<string> s = new List<string>(); 

    for (int i = 0; i < 80; ++i) 
    { 
     s.Add("col" + i); 
    } 

    for (int i = 0; i < s.Count; ++i) 
    { 
     dataTable.Columns.Add(new DataColumn(s[i], typeof(string))); 

     Binding items = new Binding(); 
     PropertyPath path = new PropertyPath(dataTable.Columns[i].ColumnName); 
     items.Path = path; 

     DataGridColumn dc = new DataGridTextColumn() 
     { 
      Header = dataTable.Columns[i].ColumnName, 
      Width=70, 
      Binding = items 
     }; 

     this.dataGrid.Columns.Add(dc); 
    } 

    viewModel.dataGrid = dataTable; 
    this.dataGrid.DataContext = viewModel.dataGrid; 

    for (int i = 0; i < 1000; ++i) 
    { 
     var row = dataTable.NewRow(); 
     for (int j = 0; j < s.Count; ++j) 
     { 
      row[s[j]] = "text" + s[j] + j; 
     } 
     dataTable.Rows.Add(row); 
    } 

以上是簡單地通過結合各頭產生相同數量的列的樣品。 當這加載時,我得到所有元素正確顯示,但滾動非常緩慢。我知道有人可能會說我呈現了80列和1000個元素,但我在窗體表單應用程序中顯示了超過10000個,並且沒有任何問題。 與Windows窗體的唯一區別是,而不是使用綁定,我每次更新它時將dataGridView的dataSource設置爲DataTable。 任何想法?我究竟做錯了什麼?

回答

2

嘗試設置EnableRowVirtualization = True