【问题标题】:Wait for itemcontrol to render等待 itemcontrol 渲染
【发布时间】:2012-11-18 23:09:51
【问题描述】:

我的 ItemControl 通过 ItemsSource 绑定到 ObservableCollection<...>。当我填充集合时,程序需要一些(一点,但仍然需要一些)时间来创建正确的 ItemTemplate 对象并将它们呈现在我的 ItemsControl 中。如果我的 ItemsControl 已经结束创建(渲染、显示)通过 ItemsSource 绑定的集合,有什么方法可以知道吗? 请帮忙。我真的很需要这个答案。在填充 ObservableCollection 之后,我想处理这个集合的对象。不幸的是,一些信息正在丢失,因为 ItemsControl 仍在创建它的项目(ItemTemplates)。

*编辑 为了更好地理解: ObservableCollection<...> MyCollection 绑定到 MyItemsControl 的 ItemsSource。

我运行代码:

for(int i = 0; i < ...; i++)
{
    MyCollection.Add(MyItem);
}

// await Task.Delay(100) // If this line is not commented, everything works fine...
// ..but I don't think it's a good idea to solve that problem in this way.

foreach(Object o in MyCollection)
{
    // The line under, is faster that the ItemsControl rendering...
    // ...so the Work is not displayed.
    o.DoSomeWorkThatShouldBeDisplayedInItemTemplateOfMyItemsControl
}

*编辑 2 已解决:LayoutUpdated 事件

【问题讨论】:

  • DoSomeWorkThatShouldBeDisplayedInItemTemplateOfMyItemsControl 具体在做什么?
  • 更改状态,MyItem 的 Enum 属性。这应该会导致 ItemTemplate 的 VisualState 发生变化。

标签: wpf data-binding itemscontrol itemrenderer itemtemplate


【解决方案1】:

我明白了。 LayoutUpdated 事件是我需要的。在另一个论坛上找到的,但希望以后能对某人有所帮助。

【讨论】:

  • 也许只有我一个人,但可能需要更多关于如何帮助您的细节来帮助其他人。
  • 所以,我需要知道 ItemsControl 何时结束呈现其项目。并且 LayoutUpdated 是它工作时引发的事件。当 ItemsControl 中的更改被“应用(项目被更改/添加/删除,并呈现”时,它被提出了。所以我在那个事件中做我的 o.DoSomeWorkThat...。不幸的是,它并不完美,因为它每次都会被提出进行了更改。我希望有一个事件,该事件在创建指定元素时引发。但这对我来说是一个很好的起点。
【解决方案2】:

试试System.Windows.Forms.Application.DoEvents();

【讨论】:

  • 什么??这不是winforms。
  • 去 Dispatcher.BeginInvoke(new Action(() => {}), DispatcherPriority.Background);而是。
猜你喜欢
  • 2019-09-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2021-11-28
  • 2021-12-13
  • 2019-03-06
  • 2014-01-16
相关资源
最近更新 更多