Hello WPF Forum,
I have a collection of documents in a Tabbed Document Interface, each document containing a list box with a list of connections. When I select various tabs, the items in the list box get reset (but in an indeterministic manner, just by changing the active tab) to the first element. I've confirmed that each listbox has it's own CollectionView (by observingGetDefaultView.GetHashCode() is unique for each document). IsSynchronizedWithCurrentItem="true" for the ListBox.
I've based my implementation to that from Josh Smith in 2009 from the WPF blogs, so that when the user creates a new document, it creates a new tab based on a collectionObservableCollection<ConnectionViewModel> Connections.
<Window x:Class="InetClient.MainWindow" ...><Window.DataContext><vm:InetClientViewModel x:Name="viewModel"/></Window.DataContext>
...
<TabControl x:Name="tabConnections"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=Connections}"><TabControl.ItemTemplate><!-- This defines the tab header --><DataTemplate><DockPanel><controls:CrossButton Command="{Binding Path=CloseConnection, ElementName=viewModel}"
CommandParameter="{Binding}"
DockPanel.Dock="Right"/><ContentPresenter Content="{Binding Path=Name}"/></DockPanel></DataTemplate></TabControl.ItemTemplate><TabControl.ContentTemplate><!-- This defines the tab content --><DataTemplate><v:ConnectionView/></DataTemplate></TabControl.ContentTemplate></TabControl></Window>Each ConnectionViewModel object has a collection IList<ConnectionInfo> ConnectionTypes, for which ConnectionView.xaml (a user control) displays. Below is ConnectionView.xaml:
<UserControl ... ><ListBox IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=ConnectionTypes}"
SelectedValuePath="Settings"><ListBox.ItemTemplate><DataTemplate><TextBlock Text="{Binding Path=Name}"/></DataTemplate></ListBox.ItemTemplate></ListBox><!-- See MainWindowResources for a mapping from the ViewModel object given as the binding
to the View.xaml element --><GroupBox Grid.Row="0" Grid.Column="1" Header="Connection Settings" Content="{Binding Path=ConnectionTypes/Settings}"/>
...
</UserControl>I can create and close the tabs as required, selecting the items in the list map to a "Settings" object which is in my ViewModel and I have a<DataTemplate DataType="{x:Type ...}"> to tell WPF which View.xaml to use. I don't have any code-behind content.
WPF is pretty steep so it could be that I've missed something in all my reading.
Thanks
Jason.