This shouldn’t be any problem. Every other part is actually placed within the bounds of the background node, so the background node itself should mark the bounds of the diagram. In my test case, that is a square of about 600x600 px. The viewport has a size of about 1700x900px. So the diagram should be stretched by 1.5 and then centered horizontally (when I set the position to 400x0 px).
A little background information. We have a navigation with subsections, somewhat like this:
- Apartments
- Level 1
- Level 2
- Rooms
- Level 1
- Level 2
- Components
- Level 1
- Level 2
Every top navigation level has its own View and its own Diagram, so when I switch from Apartments to Rooms, a new Diagram is initialized.
Each 2nd level navigation does call OnNavigatedTo but remains in the same View and keeps the same Diagram. Each 2nd level navigation point also claims one background layer, one node layer and optionally one link layer (we don’t need links in the apartment section, so there is none).
Each background layer contains a single node with this template:
<DataTemplate x:Key="BackgroundNodeTemplate">
<go:NodePanel go:Part.LayerName="{Binding Path=Data.Layer.Id}"
go:Part.LayoutId="None"
go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}"
IsHitTestVisible="False"
go:Part.Visible="{Binding Path=Data.IsVisible}"
go:Node.RotationAngle="{Binding Path=Data.Angle}">
<Image Source="{Binding Path=Data.GroundPlanImagePath}"
Width="{Binding Path=Data.Width}"
Height="{Binding Path=Data.Height}" />
</go:NodePanel>
</DataTemplate>
Data.GroundPlanImagePath
points to the location of a .png image file.
Now, when I switch the top level navigation, a new diagram is initialized, I’m waiting for the Diagram.InitialLayoutCompleted
event and then set Diagram.Panel.Scale
and Diagram.Panel.CenterPart(...)
. This seems to work so far.
When I switch the 2nd level navigation point, I set the Layer.Visibilty
for the navigation point’s corresponding layer to Visible
and for all others to Hidden
in the OnNavigatedTo
method and afterwards I try to scale and center again, which does not work. I suspect setting the visibility causes WPF to actually load the image from the file, which does take too much time and thus is not finished when I try to center so the only thing that happens is the Diagram automatically moving the background node into the Viewport’s bounds, but no centering.
In the latter case, is there an event I could hook up to? I tried LayoutCompleted
but that only gets hooked the first time. Would it make sense to set the Layer.Visibility
to Collapsed or set the background node’s visibility to get the LayoutCompleted
event to fire?