I have an app which features a docksite as part of the main window layout. I use documents (tabs) to partition the user view of the database.
This works great if I bind the DocumentItemsSource to a list of view models, each of which contains data relevant to a particular partition. However, I have found that this doesn't play nicely with the deserialisation behaviour: A call to Deserialize modifies the backing list of VMs by removing those not present in the layout information.
The problem shows up if the database is modified while the app is not running, or if I connect the app to a different database which has a different list of partitions.
Is there a way I can have the layout serialisation only apply to documents that are present, and leave the extra items?
Here is a simplified example of what I'm doing (some boilerplate - like constructor with only InitializeComponent() - excluded):
MainWindowVM.cs
using GalaSoft.MvvmLight;
MainWindowVm : ViewModelBase
{
MainWindowVm()
{
DocumentVms.Add(new OverallViewModel());
using (var ctx = new MyDataContext())
foreach (var dataSet in ctx.mainPartitions)
DocumentVms.Add(new MyPartitionVm(dataSet));
}
ObservableCollection<ViewModelBase> _documentVms = ObservableCollection<ViewModelBase>();
ObservableCollection<ViewModelBase> DocumentVms
{
get => _documentVms;
set => Set(ref _documentsVms, value);
}
}
MainWindow.xaml docksite definition:
<docking:DockSite x:Name="dockSite"
AreNewTabsInsertedBeforeExistingTabs="False"
DocumentItemsSource="{Binding DocumentVms}">
<docking:DockSite.Resources>
<DataTemplate DataType="{x:Type vm:OverallViewModel}">
<vw:OverallView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:PartitionVm}">
<vw:PartitionView/>
</DataTemplate>
</docking:DockSite.Resources>
<docking:DockSite.DocumentItemContainerStyle>
<Style TargetType="{x:Type docking:DocumentWindow}">
<Setter Property="IsOpen" Value="True"/>
<Setter Property="Title" Value="{Binding Title}"/>
<Setter Property="CanClose" Value="False"/>
<Setter Property="SerializationId" Value="{Binding Title}"/>
</Style>
</docking:DockSite.DocumentItemContainerStyle>
<!-- Workspace -->
<docking:Workspace >
<docking:TabbedMdiHost >
<docking:TabbedMdiContainer x:Name="tabs">
</docking:TabbedMdiContainer>
</docking:TabbedMdiHost>
</docking:Workspace>
</docking:DockSite>
MainWindow.xaml.cs
MainWindow : Window
{
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var serializer = ServiceManager.Resolve<DockSiteLayoutSerializer>();
serializer.SerializationBehavior = DockSiteSerializationBehavior.All;
serializer.DocumentWindowDeserializationBehavior = DockingWindowDeserializationBehavior.LazyLoad;
serializer.ToolWindowDeserializationBehavior = DockingWindowDeserializationBehavior.LazyLoad;
if (File.Exists("SerializationTest.xml"))
serializer.LoadFromFile("SerializationTest.xml", dockSite);
}
private void Window_Closing(object sender, CancelEventArgs e)
{
var serializer = ServiceManager.Resolve<DockSiteLayoutSerializer>();
serializer.SerializationBehavior = DockSiteSerializationBehavior.All;
serializer.DocumentWindowDeserializationBehavior = DockingWindowDeserializationBehavior.LazyLoad;
serializer.ToolWindowDeserializationBehavior = DockingWindowDeserializationBehavior.LazyLoad;
serializer.SaveToFile("SerializationTest.xml", dockSite);
}
}
App.xaml.cs:
public partial class App : Application
{
private async void Application_Startup(object sender, StartupEventArgs e)
{
var mainWnd = new MainWindow();
mainWnd.DataContext = new MainWindowVm()
mainWnd.Show();
}
}
Thanks in advance!