Custom ViewModel and Deserialization

Docking/MDI for WPF Forum

Posted 5 months ago by Tommaso Giachi
Version: 22.1.1

Hello Actipro,

I'm currently trying before buy the WPF suite and have a question regarding serialization. (I searched the forum but found nothing that could help me)

<DataTemplate x:Key="DockingWindowContentDataTemplate">
<ContentPresenter Content="{Binding View}"/>

<Style x:Key="DockingWindowStyle" TargetType="docking:DockingWindow">
<!--<Setter Property="Description" Value="{Binding Path=Description, Mode=TwoWay}" />-->
<Setter Property="ImageSource" Value="{Binding Path=Icon, Converter={StaticResource ImageSourceConverter}, Mode=TwoWay}" />

<Setter Property="IsActive" Value="{Binding Path=ViewModel.IsActive, Mode=TwoWay}" />
<Setter Property="IsOpen" Value="{Binding Path=ViewModel.IsOpen, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding Path=ViewModel.IsSelected, Mode=TwoWay}" />
<Setter Property="SerializationId" Value="{Binding Path=Id, Mode=TwoWay}" />
<Setter Property="Title" Value="{Binding Path=ViewModel.Title, Mode=TwoWay}" />
<Setter Property="WindowGroupName" Value="{Binding Path=ViewModel.WindowGroupName, Mode=TwoWay}" />

<Style x:Key="ToolWindowStyle" TargetType="docking:ToolWindow" BasedOn="{StaticResource DockingWindowStyle}">
<Setter Property="DefaultDockSide" Value="{Binding Path=ViewModel.DockSide, Mode=TwoWay, Converter={StaticResource ToolItemDockSideConverter}}" />
<Setter Property="State" Value="{Binding Path=ViewModel.PositionState, Mode=TwoWay, Converter={StaticResource ToolItemStateConverter}}" />

With docksite:

<docking:DockSite x:Name="DockSite" DocumentItemTemplate="{StaticResource DockingWindowContentDataTemplate}"
DocumentItemContainerStyle="{StaticResource DockingWindowStyle}"
ToolItemContainerStyle="{StaticResource ToolWindowStyle}"
ToolItemTemplate="{StaticResource DockingWindowContentDataTemplate}"
DockPanel.Dock="Top" MdiKind="Standard" DocumentItemsSource="{Binding Documents}" ToolItemsSource="{Binding Tools}">
<docking:StandardMdiHost />
<docking:ToolWindowContainer />


In my DataContext I add Documents and Tools and all works fine.

How I can do deserilization and resolve (via ioc container) view model types (i have also unique serializationId for each tools/documents) ?

I also try to assign Context and Content during DockingWindowDeserialization but i lose Style and DataTemplate

var tool = await _windowService.BuildTool(serializedTool.ViewModelType);
tool.Id = serializedTool.Id;

// args.Window.Content = tool.View;

if (tool.ToolType == ToolType.Tool)

args.Window.DataContext = tool.ViewModel;
args.Window.Content = tool.View;
args.Window.Title = tool.Title;
await tool.ViewModel.OnShow();
args.Handled = true;

Can you help me? Thank you!


[Modified 5 months ago]

Comments (1)

Posted 5 months ago by Actipro Software Support - Cleveland, OH, USA

Hi Tommaso,

Do I understand correctly that your initial MVVM setup works fine with the first two code sections you posted?  And the question is that when deserializing a layout, you need to be able to restore various document and tool windows, but that isn't working right now?

First, one quick suggestion on the DockSite XAML.  You can remove the SplitContainer and ToolWindowContainer and just leave the Workspace/StandardMdiHost.  Layout deserialization will create splits and tool window containers as needed.

If you already have the view-models in the DockSite.DocumentItemsSource and ToolItemsSource collections when starting layout deserialization, and your Style is correct that assigns the SerializationId, then layout deserialization should be locating the generated DocumentWindow and ToolWindow containers with matching SerializationIds as what is in the layout XML data and matching those up successfully.

Do you not see that happening?  Or do you NOT yet have the view-models in the DockSite and are trying to add them dynamically in a DockingWindowDeserializing event handler based on the e.Node.SerializationId that is passed?

Actipro Software Support

The latest build of this product (v22.1.3) was released 23 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.