如何在“绑定时间"获取 ListBox 中项目的 ListBoxItem [英] How to get the ListBoxItem for an item in ListBox on "bind-time"

查看:28
本文介绍了如何在“绑定时间"获取 ListBox 中项目的 ListBoxItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 Foo 对象的 ListBox,并且基于某些事件,我禁用/启用了 ListBox 中的 ListBoxItems.使用 ListBox.Items 属性,我找到了 Foo 对象,据我所知,我需要使用以下函数来获取 Foo 的 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
}

实际上我有一个自定义控件 FilteringListBox 继承 ListBox 并为其添加了一个额外的属性.上面的代码在自定义控件后面的代码中,并且在创建 FilteringListBox 时工作得很好.然而,我的问题是,当某些属性被绑定时,我会尝试这样做.我有一个属性 FilteringCollection 和一个在绑定时触发的 PropertyCallback.在此回调中,我将存储 FilteringCollection,但我还将执行初始过滤 - 运行集合并禁用任何表示 FilteringCollection 中的 Foo 的 ListBoxItem.

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.

这就是我遇到问题的地方.我找到了所有的 Foo,所以我验证了 ItemsSource 是否已设置,但是执行 ItemContainerGenerator.ContainerFromItem 我得到了 null.就像尚未创建 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?或者——如果我不能;是否有一些我可以覆盖的事件告诉我 ListBox 已完成创建 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(); 
}

这篇关于如何在“绑定时间"获取 ListBox 中项目的 ListBoxItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆