• <legend id='dzHfx'><style id='dzHfx'><dir id='dzHfx'><q id='dzHfx'></q></dir></style></legend>
    1. <small id='dzHfx'></small><noframes id='dzHfx'>

        <tfoot id='dzHfx'></tfoot>
          <bdo id='dzHfx'></bdo><ul id='dzHfx'></ul>

        <i id='dzHfx'><tr id='dzHfx'><dt id='dzHfx'><q id='dzHfx'><span id='dzHfx'><b id='dzHfx'><form id='dzHfx'><ins id='dzHfx'></ins><ul id='dzHfx'></ul><sub id='dzHfx'></sub></form><legend id='dzHfx'></legend><bdo id='dzHfx'><pre id='dzHfx'><center id='dzHfx'></center></pre></bdo></b><th id='dzHfx'></th></span></q></dt></tr></i><div id='dzHfx'><tfoot id='dzHfx'></tfoot><dl id='dzHfx'><fieldset id='dzHfx'></fieldset></dl></div>
      1. ItemContainerGenerator.ContainerFromItem 如何处理分组列表?

        How does ItemContainerGenerator.ContainerFromItem work with a grouped list?(ItemContainerGenerator.ContainerFromItem 如何处理分组列表?)
        • <bdo id='uBQdG'></bdo><ul id='uBQdG'></ul>
          1. <i id='uBQdG'><tr id='uBQdG'><dt id='uBQdG'><q id='uBQdG'><span id='uBQdG'><b id='uBQdG'><form id='uBQdG'><ins id='uBQdG'></ins><ul id='uBQdG'></ul><sub id='uBQdG'></sub></form><legend id='uBQdG'></legend><bdo id='uBQdG'><pre id='uBQdG'><center id='uBQdG'></center></pre></bdo></b><th id='uBQdG'></th></span></q></dt></tr></i><div id='uBQdG'><tfoot id='uBQdG'></tfoot><dl id='uBQdG'><fieldset id='uBQdG'></fieldset></dl></div>

            <small id='uBQdG'></small><noframes id='uBQdG'>

              <tbody id='uBQdG'></tbody>
          2. <tfoot id='uBQdG'></tfoot>
              <legend id='uBQdG'><style id='uBQdG'><dir id='uBQdG'><q id='uBQdG'></q></dir></style></legend>

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

                  问题描述

                  我有一个 ListBox,直到最近才显示一个平面的项目列表.我能够使用 myList.ItemContainerGenerator.ConainerFromItem(thing) 来检索列表中托管事物"的 ListBoxItem.

                  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.

                  本周我稍微修改了 ListBox,因为它为其项目绑定的 CollectionViewSource 已启用分组.现在 ListBox 中的项目被分组在漂亮的标题下.

                  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.

                  但是,自从这样做之后,ItemContainerGenerator.ContainerFromItem 已经停止工作 - 即使对于我知道在 ListBox 中的项目,它也会返回 null.见鬼 - 即使 ListBox 填充了许多项目,ContainerFromIndex(0) 也会返回 null!

                  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!

                  如何从显示分组项的 ListBox 中检索 ListBoxItem?

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

                  这是精简示例的 XAML 和代码隐藏.这会引发 NullReferenceException,因为即使列表中有四个项目,ContainerFromIndex(1) 也会返回 null.

                  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();
                      }
                  }
                  

                  推荐答案

                  去监听 ItemsGenerator.StatusChanged 事件并做出反应,并等到 ItemContainers 生成在您可以使用 ContainerFromElement 访问它们之前.

                  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.

                  进一步搜索,我发现 MSDN 论坛中的一个主题 来自有同样问题的人.当一个设置了 GroupStyle 时,这似乎是 WPF 中的一个错误.解决方案是在渲染过程之后对 ItemGenerator 的访问进行踢.以下是您的问题的代码.我试过了,它有效:

                  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 如何处理分组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                  How do I get at the listbox item#39;s quot;keyquot; in c# winforms app?(如何获取列表框项目的“键?在 c# winforms 应用程序中?)
                1. <small id='w5W8d'></small><noframes id='w5W8d'>

                    <tbody id='w5W8d'></tbody>

                  <i id='w5W8d'><tr id='w5W8d'><dt id='w5W8d'><q id='w5W8d'><span id='w5W8d'><b id='w5W8d'><form id='w5W8d'><ins id='w5W8d'></ins><ul id='w5W8d'></ul><sub id='w5W8d'></sub></form><legend id='w5W8d'></legend><bdo id='w5W8d'><pre id='w5W8d'><center id='w5W8d'></center></pre></bdo></b><th id='w5W8d'></th></span></q></dt></tr></i><div id='w5W8d'><tfoot id='w5W8d'></tfoot><dl id='w5W8d'><fieldset id='w5W8d'></fieldset></dl></div>
                  <tfoot id='w5W8d'></tfoot><legend id='w5W8d'><style id='w5W8d'><dir id='w5W8d'><q id='w5W8d'></q></dir></style></legend>
                    <bdo id='w5W8d'></bdo><ul id='w5W8d'></ul>