Application error when changing tabs containing ListBoxes with ItemTemplates

I have a WPC TabControl with two TabItems. Each TabItem contains a ListBox with a separate ObservableCollection as its ItemsSource. Each ListBox has a different ItemTemplate.

Regardless of which TabItem I have to select on startup, that tab will show up fine, but when I click on another tab, the application crashes with an "Exception was caused by the target of the call" error pointing to the DataTemplate for the tab I am navigating to ...

If I remove the ItemTemplate from the ListBox in the tab I try to switch (and use DisplayMemberPath) everything works fine.

However, if I use the DataTemplate, whether inline or as a StaticResource or DynamicResource, it crashes when switching tabs.

Any ideas? Below is the pseudocode:


<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"
    >
<Window.Resources>
    <DataTemplate x:Key="ItemTemplateOne">
        <controls:CustomControlOne />
    </DataTemplate>
    <DataTemplate x:Key="ItemTemplateTwo">
        <controls:CustomControlTwo />
    </DataTemplate>
</Window.Resources>
<Grid>
    <TabControl Name="tabControl1">
        <TabItem Header="TabOne">
            <Grid>
                <ScrollViewer>
                    <ListBox Name="ListBoxOne" 
                             ItemsSource="{Binding}"
                             ItemTemplate="{StaticResource ItemTemplateOne}"
                             >
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel /> 
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                    </ListBox>
                </ScrollViewer>
            </Grid>
        </TabItem>
        <TabItem Header="TabTwo">
            <Grid>
                <ScrollViewer>
                    <ListBox Name="ListBoxTwo" 
                             ItemsSource="{Binding}"
                             ItemTemplate="{StaticResource ItemTemplateTwo}"
                             >
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel />
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                    </ListBox>
                </ScrollViewer>
            </Grid>
        </TabItem>
    </TabControl>
</Grid>

      

public Window1 () {InitializeComponent ();

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

      

}

0


source to share


2 answers


Regardless of which TabItem I have to select on startup, that tab will show up fine, but when I click on another tab, the app crashes with an "Exception was caused by the target of the call" error pointing to the DataTemplate for the tab I am switching to ...



Include first chance exceptions so you can know the actual exception instead of the wrapped outer exception - what goes wrong will be much clearer.

+1


source


I think the problem is that both ListBox ItemSource = "{Binding}". I think it is talking about binding to the DataContext window, but in the code you install separately.

Try declaring a CollectionViewSource (or two if you want different types between ListBoxes) in your Window.Resources. Set Observablecollection as CollectionViewSource source.



Then, in your lists, use source binding CollectionView.

If that doesn't work, you can try putting each ListBox and its associated data resources into separate UserControls.

0


source







All Articles