本文介绍了如何ItemContainerGenerator.ContainerFromItem一个分组名单的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,直到最近被展示项目的单位名单。我能够使用myList.ItemContainerGenerator.ConainerFromItem(事)检索ListBoxItem的托管列表中的东西。

这一周,我就在那的CollectionViewSource略作修改的列表框,它结合其项目已分组启用。现在,列表框内部的项目分组不错的头下面。

不过,由于这样做,ItemContainerGenerator.ContainerFromItem已停止工作 - 它返回null即使我知道是在ListBox中的项目。哎呀 - ContainerFromIndex(0)返回null,即使在列表框填充了许多项目

我如何检索一个ListBox多数民众赞成显示分组的项目一个ListBoxItem的?

编辑:这是C-背后的XAML和$ C $为一个精简的例子。这就提出了一个NullReferenceException,因为ContainerFromIndex(1)返回null即使有四个项目在列表中。

XAML:

 <窗​​口x:类=WpfApplication1.Window1
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    的xmlns:SCM =CLR的命名空间:System.ComponentModel;装配= WindowsBase
    标题=窗口1>

    < Window.Resources>
        < XmlDataProvider X:关键=myTasks的XPath =任务/任务>
            &所述; X:扩展数据>
                <任务的xmlns =>
                    <任务名称=杂货类型=首页/>
                    <任务名称=清洗类型=首页/>
                    <任务名称=编码类型=工作/>
                    <任务名称=会议TYPE =工作/>
                < /任务>
            < / X:扩展数据>
        < / XmlDataProvider>

        < CollectionViewSource X:关键=mySortedTasks来源={的StaticResource myTasks}>
            < CollectionViewSource.SortDescriptions>
                < SCM:SortDescription属性名=@类型/>
                < SCM:SortDescription属性名=@名称/>
            < /CollectionViewSource.SortDescriptions>

            < CollectionViewSource.GroupDesc​​riptions>
                < PropertyGroupDesc​​ription属性名=@类型/>
            < /CollectionViewSource.GroupDesc​​riptions>
        < / CollectionViewSource>
    < /Window.Resources>

    <列表框
        X:名称=listBox1中
        的ItemsSource ={绑定源= {的StaticResource mySortedTasks}}
        的DisplayMemberPath =@名称
        >
        < ListBox.GroupStyle>
            < GroupStyle>
                < GroupStyle.HeaderTemplate>
                    <的DataTemplate>
                        < TextBlock的文本={结合名}/>
                    < / DataTemplate中>
                < /GroupStyle.HeaderTemplate>
            < / GroupStyle>
        < /ListBox.GroupStyle>
    < /列表框>
< /窗>
 

CS:

 公共窗口1()
{
    的InitializeComponent();
    listBox1.ItemContainerGenerator.StatusChanged + = ItemContainerGenerator_StatusChanged;
}

无效ItemContainerGenerator_StatusChanged(对象发件人,EventArgs的)
{
    如果(listBox1.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
    {
        listBox1.ItemContainerGenerator.StatusChanged  -  = ItemContainerGenerator_StatusChanged;

        变种I = listBox1.ItemContainerGenerator.ContainerFromIndex(1),为ListBoxItem中;

        //选择和键盘,专注的第二个项目
        i.IsSelected = TRUE;
        i.Focus();
    }
}
 

解决方案

您的有无以听取并做出反应 ItemsGenerator.StatusChanged 事件和等到产生了ItemContainers之前,你可以用ContainerFromElement访问它们。


进一步的搜索,我发现a线程在MSDN论坛从谁的人有同样的问题。这似乎是在WPF中的一个错误,当一个人有GroupStyle集。解决的办法是踢的ItemGenerator的描绘处理后的访问。下面是code的提问。我想这一点,和它的作品:

 无效ItemContainerGenerator_StatusChanged(对象发件人,EventArgs的)
    {
        如果(listBox1.ItemContainerGenerator.Status
            == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
        {
            listBox1.ItemContainerGenerator.StatusChanged
                 -  = ItemContainerGenerator_StatusChanged;
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input,
                新的行动(DelayedAction));
        }
    }

    无效DelayedAction()
    {
        变种I = listBox1.ItemContainerGenerator.ContainerFromIndex(1),为ListBoxItem中;

        //选择和键盘,专注的第二个项目
        i.IsSelected = TRUE;
        i.Focus();
    }
 

I have a ListBox which until recently was displaying a flat list of items. I was able to use myList.ItemContainerGenerator.ConainerFromItem(thing) to retrieve the ListBoxItem hosting "thing" in the list.

This week I've modified the ListBox slightly in that the CollectionViewSource that it binds to for its items has grouping enabled. Now the items within the ListBox are grouped underneath nice headers.

However, since doing this, ItemContainerGenerator.ContainerFromItem has stopped working - it returns null even for items I know are in the ListBox. Heck - ContainerFromIndex(0) is returning null even when the ListBox is populated with many items!

How do I retrieve a ListBoxItem from a ListBox that's displaying grouped items?

Edit: Here's the XAML and code-behind for a trimmed-down example. This raises a NullReferenceException because ContainerFromIndex(1) is returning null even though there are four items in the list.

XAML:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    Title="Window1">

    <Window.Resources>
        <XmlDataProvider x:Key="myTasks" XPath="Tasks/Task">
            <x:XData>
                <Tasks xmlns="">
                    <Task Name="Groceries" Type="Home"/>
                    <Task Name="Cleaning" Type="Home"/>
                    <Task Name="Coding" Type="Work"/>
                    <Task Name="Meetings" Type="Work"/>
                </Tasks>
            </x:XData>
        </XmlDataProvider>

        <CollectionViewSource x:Key="mySortedTasks" Source="{StaticResource myTasks}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="@Type" />
                <scm:SortDescription PropertyName="@Name" />
            </CollectionViewSource.SortDescriptions>

            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="@Type" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>

    <ListBox 
        x:Name="listBox1" 
        ItemsSource="{Binding Source={StaticResource mySortedTasks}}" 
        DisplayMemberPath="@Name"
        >
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListBox.GroupStyle>
    </ListBox>
</Window>

CS:

public Window1()
{
    InitializeComponent();
    listBox1.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
}

void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
    if (listBox1.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
    {
        listBox1.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;

        var i = listBox1.ItemContainerGenerator.ContainerFromIndex(1) as ListBoxItem;

        // select and keyboard-focus the second item
        i.IsSelected = true;
        i.Focus();
    }
}
解决方案

You have to listen and react to the ItemsGenerator.StatusChanged Event and wait until the ItemContainers are generated before you can access them with ContainerFromElement.


Searching further, I've found a thread in the MSDN forum from someone who has the same problem. This seems to be a bug in WPF, when one has a GroupStyle set. The solution is to punt the access of the ItemGenerator after the rendering process. Below is the code for your question. I tried this, and it works:

    void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
    {
        if (listBox1.ItemContainerGenerator.Status
            == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
        {
            listBox1.ItemContainerGenerator.StatusChanged
                -= ItemContainerGenerator_StatusChanged;
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input,
                new Action(DelayedAction));
        }
    }

    void DelayedAction()
    {
        var i = listBox1.ItemContainerGenerator.ContainerFromIndex(1) as ListBoxItem;

        // select and keyboard-focus the second item
        i.IsSelected = true;
        i.Focus();
    }

这篇关于如何ItemContainerGenerator.ContainerFromItem一个分组名单的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:48