如何在F#中以定时间隔刷新DataGrid?

时间:2022-10-14 20:57:49

I have a background thread updating an array. At timed intervals I call myDataGrid.Items.Refresh(). However nothing changes on the screen. But when I for instance click on the column heading of the data grid on the screen the information is actualized immediately.

我有一个后台线程更新数组。在定时间隔,我调用myDataGrid.Items.Refresh()。然而,屏幕上没有任何变化。但是当我例如点击屏幕上数据网格的列标题时,信息立即实现。

I like to see the changes on the screen at timed intervals, for instance every 2 seconds. What am I missing?

我希望以定时间隔在屏幕上看到更改,例如每2秒钟。我错过了什么?

Here is the code fragment in F# that shows the situation:

这是F#中的代码片段,显示了这种情况:

...

let win = new Window()
let grid = DataGrid()
grid.HeadersVisibility <- DataGridHeadersVisibility.All
grid.ItemsSource <- myArray
win.Content <- new ScrollViewer(Content=grid)
win.Show()
...
// Background thread A
//  updating myArray

... 

// Background thread B
let updateDataGrid = 
  grid.Items.Refresh()
  Thread.Sleep(5000)
  updateDataGrid

...

[<STAThread>]
do 
  let app = new Application()
  app.Run() |> ignore

3 个解决方案

#1


Have you tried a DispatcherTimer? (code below is in C#)

你试过DispatcherTimer吗? (以下代码在C#中)

timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer1_Tick;

Prevent the usage of Thread.Sleep.

防止使用Thread.Sleep。

#2


Can't use formatting in comments so here my response.

不能在评论中使用格式,所以在这里我的回复。

The timer actually works, if I write:

如果我写的话,计时器实际上是有效的:

[<STAThread>]
do 
  let app = new Application()
  let timer = new DispatcherTimer()
  timer.Interval <- TimeSpan.FromSeconds(2.)
  timer.Tick.Add (fun _ -> grid.Items.Refresh())
  timer.Start()
  app.Run() |> ignore

However now the problem is almost reversed, it automatically updates until I click on any of the column headings to sort. After that there are no more refreshes.

但是现在问题几乎已经逆转,它会自动更新,直到我点击任何列标题进行排序。之后,没有更多的刷新。

However if I perform this trick:

但是如果我执行这个技巧:

  timer.Tick.Add (fun _ ->
                    grid.ItemsSource <- null
                    grid.ItemsSource <- myArray
                    grid.Items.Refresh())

it refreshes fine, but loses the sort ordering.

它刷新很好,但失去了排序顺序。

How to maintain the sort order? I can easily sort the arrray but I do like the user to sort by himself as well.

如何维护排序顺序?我可以轻松地对arrray进行排序,但我确实希望用户自己排序。

#3


Since you're already using WPF, is it possible to turn that array into an ObservableCollection? Last I heard, the DataGrids support it. If the objects in the collection are already DependencyObjects, then their DependancyProperties should automatically update in the grid. If not, you can reinsert them into the collection manually.

由于您已经在使用WPF,是否可以将该数组转换为ObservableCollection?最后我听说,DataGrids支持它。如果集合中的对象已经是DependencyObjects,那么它们的DependancyProperties应该在网格中自动更新。如果没有,您可以手动将它们重新插入集合中。

#1


Have you tried a DispatcherTimer? (code below is in C#)

你试过DispatcherTimer吗? (以下代码在C#中)

timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer1_Tick;

Prevent the usage of Thread.Sleep.

防止使用Thread.Sleep。

#2


Can't use formatting in comments so here my response.

不能在评论中使用格式,所以在这里我的回复。

The timer actually works, if I write:

如果我写的话,计时器实际上是有效的:

[<STAThread>]
do 
  let app = new Application()
  let timer = new DispatcherTimer()
  timer.Interval <- TimeSpan.FromSeconds(2.)
  timer.Tick.Add (fun _ -> grid.Items.Refresh())
  timer.Start()
  app.Run() |> ignore

However now the problem is almost reversed, it automatically updates until I click on any of the column headings to sort. After that there are no more refreshes.

但是现在问题几乎已经逆转,它会自动更新,直到我点击任何列标题进行排序。之后,没有更多的刷新。

However if I perform this trick:

但是如果我执行这个技巧:

  timer.Tick.Add (fun _ ->
                    grid.ItemsSource <- null
                    grid.ItemsSource <- myArray
                    grid.Items.Refresh())

it refreshes fine, but loses the sort ordering.

它刷新很好,但失去了排序顺序。

How to maintain the sort order? I can easily sort the arrray but I do like the user to sort by himself as well.

如何维护排序顺序?我可以轻松地对arrray进行排序,但我确实希望用户自己排序。

#3


Since you're already using WPF, is it possible to turn that array into an ObservableCollection? Last I heard, the DataGrids support it. If the objects in the collection are already DependencyObjects, then their DependancyProperties should automatically update in the grid. If not, you can reinsert them into the collection manually.

由于您已经在使用WPF,是否可以将该数组转换为ObservableCollection?最后我听说,DataGrids支持它。如果集合中的对象已经是DependencyObjects,那么它们的DependancyProperties应该在网格中自动更新。如果没有,您可以手动将它们重新插入集合中。