这是一个简单的DataGrid
基准:
open System.Windows
let mutable i = 0
let timer = System.Diagnostics.Stopwatch.StartNew()
[<System.STAThreadAttribute>]
do
let grid = Controls.DataGrid()
grid.AutoGenerateColumns <- false
for i in 1..50 do
let c = Controls.DataGridTextColumn()
c.Binding <- Data.Binding()
c.Header <- string i
grid.Columns.Add c
grid.ItemsSource <- Array.init 1000 string
let window = Window(Content=grid)
window.WindowState <- WindowState.Maximized
Media.CompositionTarget.Rendering.Add(fun _ ->
i <- i + 38
grid.ScrollIntoView grid.Items.[i % 1000]
window.Title <- sprintf "%gfps" (float i / timer.Elapsed.TotalSeconds / 38.0)
if i > 10000 then
i <- 1
timer.Restart())
Application().Run window
|> ignore
我从中获得了糟糕的性能(在 4x 3.4GHz Intel Core i7 上为 6 帧/秒)。如何优化?
我在网上找到了一些建议,但没有一个有任何显着差异:
grid.EnableColumnVirtualization <- true
grid.EnableRowVirtualization <- true
grid.ColumnWidth <- Controls.DataGridLength 27.0
grid.RowHeight <- 18.0
...
if i=0 then
let border = Media.VisualTreeHelper.GetChild(grid, 0) :?> Controls.Border
let scroll = border.Child :?> Controls.ScrollViewer
scroll.IsDeferredScrollingEnabled <- true