如何获得ListBoxItem中关于&QUOT在ListBox中的项目;结合时间和QUOT;如何获得、时间、项目、QUOT

2023-09-03 06:35:18 作者:至少还有你

我有富对象列表框,并根据一些事件我禁用/启用ListBox中ListBoxItems。使用ListBox.Items财产我觉得富对象,并从我了解我需要使用下面的函数来获得对富的ListBoxItem的容器。正确?

I have a ListBox with Foo objects, and based on some events I disable/enable the ListBoxItems in the ListBox. Using the ListBox.Items property I find Foo objects, and from what I've understood I need to use the following function to get the ListBoxItem container for the Foo. Correct?

foreach (var item in Items)
{
    var lbi = ItemContainerGenerator.ContainerFromItem(foo) as ListBoxItem;
    // do something
}

其实我有一个继承ListBox和增加了一个额外的属性给它一个自定义的控制FilteringListBox。上述code是在code自定义控件的后面,当FilteringListBox完成正在创建工作得很好。然而,我的问题是,我尝试这样做时,一些属性绑定。我有一个属性FilteringCollection和PropertyCallback触发时,这势必。在这个回调,我将存储FilteringCollection,但我也将做初步筛选 - 通过设置集合运行,并禁用任何一个ListBoxItem重新presenting一个Foo这是在FilteringCollection。

Actually I have a custom control FilteringListBox which inherit ListBox and adds an extra property to it. The above code is in the code behind of the custom control and works just fine when the FilteringListBox is done being created. My problem however is that I try doing this when some property is bound. I have a property FilteringCollection and a PropertyCallback triggered when this is bound. In this callback I will store the FilteringCollection, but I will also do the initial filtering - running through the set collection and disable any ListBoxItem representing a Foo which is in the FilteringCollection.

这是我得到的问题。我觉得所有的FOOS,让我确认的ItemsSource设置,但这样做的ItemContainerGenerator.ContainerFromItem我得到空。这就像不创建ListBoxItems呢。不是吗?这里是我的绑定:

This is where I get problems. I find all the Foos, so I verify that the ItemsSource is set, but doing the ItemContainerGenerator.ContainerFromItem I get null. It's like the ListBoxItems aren't created yet. Aren't they? Here is my binding:

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

所以,任:我怎样才能在绑定时间的ListBoxItems?或者 - 如果做不到;有一些事件,我可以重写,告诉我的列表框完成创建ListBoxItems?试着OnInitialized没有运气...

So; either: How can I get the ListBoxItems on "bind-time"? Or - if I can't; is there some event I can override that tells me the ListBox is done creating ListBoxItems? Tried OnInitialized without luck...

推荐答案

其实一个更好的解决方案似乎是使用的 ItemContainerGenerator 。挂钩事件处理程序的创建:

Actually a better solution seems to be using the ItemContainerGenerator. Hook up an event handler on creation:

ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;

和使事件处理程序做需要做的事情:

And make the event handler do what needs to be done:

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