Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have found a solution to my problem. I was certain that I wasn't the first person to hit this issue so I continued to search StackOverflow for solutions and I stumbled upon this answer by David <a href="https://stackoverflow.com/questions/165424/how-does-itemcontainergenerator-containerfromitem-work-with-a-grouped-list/169123#169123">about how ItemContainerGenerator works with a grouped list</a>.</p> <p>David's solution was to delay accessing the <code>ItemContainerGenerator</code> until <strong>after</strong> the rendering process. </p> <p>I have implemented this solution, with a few changes that I will detail after.</p> <pre><code>private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox control = (ListBox)sender; if (control.IsGrouping) { if (control.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated) Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(DelayedBringIntoView)); else control.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged; } else control.ScrollIntoView(control.SelectedItem); } private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e) { if (ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated) return; ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged; Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(DelayedBringIntoView)); } private void DelayedBringIntoView() { var item = ItemContainerGenerator.ContainerFromItem(SelectedItem) as ListBoxItem; if (item != null) item.BringIntoView(); } </code></pre> <p>Changes:</p> <ul> <li>Only uses the <code>ItemContainerGenerator</code> approach when it <code>IsGrouping</code> is <code>true</code>, otherwise continue to use the default <code>ScrollIntoView</code>. </li> <li>Check if the <code>ItemContainerGenerator</code> is ready, if so dispatch the action, otherwise listen for the <code>ItemContainerGenerator</code> status to change.. This is important as if it is ready then the <code>StatusChanged</code> event will never fire. </li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload