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

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

问题描述

我有一个ListBox与Foo对象,并基于一些事件我禁用/启用ListBoxItems在ListBox。使用ListBox.Items属性我发现Foo对象,从我所了解的我需要使用以下函数来获取Foo的ListBoxItem容器。正确?

  foreach(项目中的项目)
{
var lbi = ItemContainerGenerator.ContainerFromItem(foo)as ListBoxItem;
//做某事
}

其实我有一个自定义控件FilteringListBox继承ListBox并添加一个额外的属性。上面的代码是在自定义控件后面的代码中,并且在FilteringListBox完成创建时工作正常。然而,我的问题是,当一些财产被绑定时,我尝试这样做。我有一个属性FilteringCollection和一个PropertyCallback触发这个绑定。在这个回调函数中,我将存储FilteringCollection,但是我也将进行初始过滤 - 运行集合集合并禁用表示FilteringCollection中的Foo的任何ListBoxItem。



这是我遇到的问题。我找到所有的Foos,所以我验证ItemsSource是否设置,但做ItemContainerGenerator.ContainerFromItem我得到null。这就像ListBoxItems尚未创建。不是吗这是我的约束:

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

所以;或者:如何在绑定时间上获取ListBoxItems?或 - 如果我不能是否有一些事件我可以覆盖,告诉我的ListBox完成创建ListBoxItems?试过OnInitialized没有运气...

解决方案

实际上一个更好的解决方案似乎是使用 ItemContainerGenerator 。在创建时挂接事件处理程序:

  ItemContainerGenerator.StatusChanged + = ItemContainerGenerator_StatusChanged; 

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

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


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
}

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.

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>

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...

解决方案

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中的“bind-time”项目的ListBoxItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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