Note that there are some explanatory texts on larger screens.

plurals
  1. POApp crashes on changing tabs that contain ListBoxes with ItemTemplates
    primarykey
    data
    text
    <p>I have a WPF TabControl with two TabItems. Each TabItem contains a ListBox with a separate ObservableCollection as its ItemsSource. Each ListBox has a different ItemTemplate. </p> <p>No matter which TabItem I set to be selected at startup, that tab will be displayed fine, but when I click on the other tab the application crashes with an 'Exception has been thrown by the target of an invocation' error pointing toward the DataTemplate for the tab I'm switching to. </p> <p>If I remove the ItemTemplate from the ListBox on the tab I'm trying to switch to (and use DisplayMemberPath) everything works fine. </p> <p>However, if I use a DataTemplate, whether it be inline or as a StaticResource or DynamicResource, it causes the crash on tab switching. </p> <p>Any ideas? Pseudo-code follows:</p> <hr> <pre><code>&lt;Window x:Class="Example.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:objects="clr-namespace:Example.CoreObjects" xmlns:controls="clr-namespace:Example.Controls" &gt; &lt;Window.Resources&gt; &lt;DataTemplate x:Key="ItemTemplateOne"&gt; &lt;controls:CustomControlOne /&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="ItemTemplateTwo"&gt; &lt;controls:CustomControlTwo /&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;TabControl Name="tabControl1"&gt; &lt;TabItem Header="TabOne"&gt; &lt;Grid&gt; &lt;ScrollViewer&gt; &lt;ListBox Name="ListBoxOne" ItemsSource="{Binding}" ItemTemplate="{StaticResource ItemTemplateOne}" &gt; &lt;ListBox.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;WrapPanel /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListBox.ItemsPanel&gt; &lt;/ListBox&gt; &lt;/ScrollViewer&gt; &lt;/Grid&gt; &lt;/TabItem&gt; &lt;TabItem Header="TabTwo"&gt; &lt;Grid&gt; &lt;ScrollViewer&gt; &lt;ListBox Name="ListBoxTwo" ItemsSource="{Binding}" ItemTemplate="{StaticResource ItemTemplateTwo}" &gt; &lt;ListBox.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;WrapPanel /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListBox.ItemsPanel&gt; &lt;/ListBox&gt; &lt;/ScrollViewer&gt; &lt;/Grid&gt; &lt;/TabItem&gt; &lt;/TabControl&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>public Window1() { InitializeComponent();</p> <pre><code>ListBoxOne.DataContext = ObservableCollectionOne; CollectionViewOne = CollectionViewSource.GetDefaultView(ObservableCollectionOne); CollectionViewOne.SortDescriptions.Add(new SortDescription("SortProperty", ListSortDirection.Descending)); ListBoxTwo.DataContext = ObservableCollectionTwo; CollectionViewTwo = CollectionViewSource.GetDefaultView(ObservableCollectionTwo); CollectionViewTwo.SortDescriptions.Add(new SortDescription("SortProperty", ListSortDirection.Descending)); </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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