2012-11-18 45 views
-1

我的ItemControl通過ItemsSource綁定到ObservableCollection < ...>。當我填充集合時,程序需要一些時間來創建正確的ItemTemplate對象並將它們呈現在我的ItemsControl中。如果我的ItemsControl已經結束創建(渲染,顯示)通過ItemsSource綁定的集合,是否有任何方法可以知道? 請幫忙。我真的需要這個答案。在填充ObservableCollection之後,我想處理這個集合的對象。不幸的是,一些信息正在丟失,因爲ItemsControl仍在創建它的項目(ItemTemplates)。等待itemcontrol呈現

* 編輯 爲了更好地理解: 的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 
} 

* EDIT 2 解決:LayoutUpdated事件

+0

它是什麼具體說明DoSomeWorkThatShouldBeDisplayedInItemTemplateOfMyItemsControl'在做什麼? –

+0

更改MyItem的狀態,Enum屬性。這應該會導致ItemTemplate的VisualState發生更改。 – jozefkarton

回答

-1

嘗試System.Windows.Forms.Application.DoEvents();

+0

什麼?這不是winforms。 –

+0

go for Dispatcher.BeginInvoke(new Action((=){}),DispatcherPriority.Background);代替。 – Manolo

0

我知道了。 LayoutUpdated事件是我需要的。在另一個論壇上發現,但希望在未來幫助某人。

+0

也許這只是我,但更多的細節,如何幫助你可能需要幫助別人。 – kenny

+0

因此,我需要知道ItemsControl何時結束渲染其項目。 LayoutUpdated是它在工作時引發的事件。當ItemsControl中的變化被應用時(項目被改變/添加/刪除,並且被渲染),所以我提出了這個問題,所以我做了o.DoSomeWorkThat ...在這種情況下,不幸的是,這並不完美,因爲每次我希望有一個事件,當創建指定的元素時會引發這個事件,但這對我來說是一個很好的起點。 – jozefkarton

相關問題