为什么 ItemContainerGenerator 返回 null? [英] Why does ItemContainerGenerator return null?

查看:32
本文介绍了为什么 ItemContainerGenerator 返回 null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListBox,我需要将它的 ControlTemplate 设置为 Virtualizing WrapPanel,它是一个扩展 VirtualizingPanel 的类,使用的样式如下所示:

I have a ListBox, and I need to set its ControlTemplate to a Virtualizing WrapPanel which is a class that extends VirtualizingPanel, using a style that looks like this:

<Style TargetType="{x:Type ListBox}" x:Key="PhotoListBoxStyle">
                <Setter Property="Foreground" Value="White" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate  TargetType="{x:Type ListBox}" >
                            <s:VirtualizingVerticalWrapPanel>
                            </s:VirtualizingVerticalWrapPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

现在,在下面 Virtualizing WrapPanel 的私有方法中,我尝试访问 this.ItemContainerGenerator,但我得到了 null 值,不知道是什么问题??

Now, in the private method of Virtualizing WrapPanel below I try to access this.ItemContainerGenerator, but I get null value, any idea what's the problem ??

private void RealizeFirstItem()
{
    IItemContainerGenerator generator = this.ItemContainerGenerator;
    GeneratorPosition pos = generator.GeneratorPositionFromIndex(0);

    using (generator.StartAt(pos, GeneratorDirection.Forward))
    {
        UIElement element = generator.GenerateNext() as UIElement;

         generator.PrepareItemContainer(element);

                    this.AddInternalChild(element);
    }
 }

推荐答案

我想我遇到了类似的问题,这对我有帮助:

I think I had a similar problem and this helped:

var necessaryChidrenTouch = this.Children;
IItemContainerGenerator generator = this.ItemContainerGenerator;

...出于某种原因,您必须触摸"子集合,以便 ItemContainerGenerator 正确初始化.

... for some reason you have to "touch" the children collection in order for the ItemContainerGenerator to initialize properly.

这篇关于为什么 ItemContainerGenerator 返回 null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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