2010-09-09 111 views
2

我有一個ListBox與Foo對象,並基於一些事件我禁用/啓用列表框中的ListBoxItems。使用ListBox.Items屬性,我發現Foo對象,並從我瞭解我需要使用以下函數獲取Foo的ListBoxItem容器。正確?如何在「綁定時間」獲取ListBox中的項目ListBoxItem

其實我有繼承ListBox和增加了一個額外的屬性給它一個自定義的控制FilteringListBox。上面的代碼位於自定義控件的後面代碼中,並且在FilteringListBox完成創建時工作得很好。然而,我的問題是,當某些財產受到約束時,我會嘗試這樣做。當綁定時,我有一個屬性FilteringCollection和一個PropertyCallback。在這個回調函數中,我將存儲FilteringCollection,但是我還將執行初始過濾 - 運行set集合並禁用表示FilteringCollection中的Foo的任何ListBoxItem。

這是我遇到問題的地方。我找到了所有的Foos,所以我確認了ItemsSource被設置了,但是做了ItemContainerGenerator.ContainerFromItem我得到了null。這就像ListBoxItems尚未創建。他們不是嗎?這裏是我的綁定:

<custom:FilteringListBox ItemsSource="{Binding AvailableFoos}" FilteringCollection="{Binding AlreadyIncludedFoos}"></custom:FilteringListBox> 

所以;要麼:我怎樣才能獲得「綁定時間」的ListBoxItems?或者 - 如果我不能;是否有一些事件我可以重寫,告訴我ListBox完成創建ListBoxItems?試圖OnInitialized沒有運氣...

回答

1

實際上更好的解決方案似乎是使用ItemContainerGenerator。掛鉤事件處理程序上創建:

ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged; 

,使事件處理程序做需要做的事情:

protected void ItemContainerGenerator_StatusChanged(object sender, System.EventArgs e) 
{ 
    if (ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated) 
     EvaluateInitialElements(); 
} 
0

事件OnRender被觸發時,該組件是準備渲染,因此在創建ListBoxItem中的。在此事件上進行過濾的初始處理似乎確保了我所需的一切都準備就緒。我評估和禁用元素,然後觸發渲染:

protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) 
{ 
    EvaluateInitialElements(); 
    base.OnRender(drawingContext); 
} 
相關問題