简体   繁体   English

如何在“绑定时间”上获取ListBox中项目的ListBoxItem

[英]How to get the ListBoxItem for an item in ListBox on “bind-time”

I have a ListBox with Foo objects, and based on some events I disable/enable the ListBoxItems in the ListBox. 我有一个带有Foo对象的ListBox,并且基于某些事件,我禁用/启用了ListBox中的ListBoxItems。 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. 使用ListBox.Items属性,我可以找到Foo对象,并且据我所知,我需要使用以下函数来获取Foo的ListBoxItem容器。 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. 实际上,我有一个自定义控件FilteringListBox,它继承了ListBox并为其添加了额外的属性。 The above code is in the code behind of the custom control and works just fine when the FilteringListBox is done being created. 上面的代码在自定义控件的后面的代码中,当完成FilteringListBox的创建时,它可以正常工作。 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. 我有一个属性FilteringCollection和绑定时触发的PropertyCallback。 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. 在此回调中,我将存储FilteringCollection,但还将进行初始过滤-在set集合中运行并禁用FilteringCollection中表示Foo的任何ListBoxItem。

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. 我找到了所有的Foos,所以我验证是否设置了ItemsSource,但是在执行ItemContainerGenerator.ContainerFromItem时我得到了null。 It's like the ListBoxItems aren't created yet. 就像尚未创建ListBoxItems一样。 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"? 任一个:如何在“绑定时间”上获取ListBoxItems? Or - if I can't; 或者-如果我做不到; is there some event I can override that tells me the ListBox is done creating ListBoxItems? 有什么我可以重写的事件可以告诉我ListBox创建完ListBoxItems吗? Tried OnInitialized without luck... 尝试过初始化没有运气...

Actually a better solution seems to be using the ItemContainerGenerator . 实际上,似乎更好的解决方案是使用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(); 
}

The event OnRender is triggered when the component is ready to be rendered, and hence the ListBoxItem's are created. 准备渲染组件时将触发事件OnRender ,因此将创建ListBoxItem。 Doing the initial handling of the filtering on this event seems to ensure that everything I need is ready. 对此事件进行过滤的初始处理似乎可以确保我需要的一切都准备就绪。 I evaluate and disable elements, and then trigger the rendering: 我评估并禁用元素,然后触发渲染:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM