I’ve got a Canvas element in a resizable window; on this canvas are a number of Image and ArcElements that are connected together. I’m trying to get the position of the images to adjust relatively in response to a resize of the window/canvas, but for some reason I can’t read the dimensions of the canvas.
The main window is defined as:
<Page>
<DockPanel LastChildFill="True">
<TextBox DockPanel.Dock="Top">Message</TextBox>
<Canvas></Canvas>
</DockPanel>
</Page>
I’ve hooked up MvvMLight’s EventToCommand so that I can route the Canvas’s LayoutUpdated or
SizeChanged events to my viewmodel; I tried databinding the Canvas’s Width and Height properties, but the dimensions always came out as zero, which meant that all the images on the canvas would appear dead centre rather than positioned as desired.
It turns out I was heading in the right direction by using MvvmLight’s EventToCommand; there is an attribute
PassEventArgsToCommandthat when set toTruesends the event args to the appropriateRelayCommandin the viewmodel. So in the viewmodel, I initialised the command thusly:and in the
RecalculateObjectPositionsmethod, I can accesse.NewSizeto find the new size of the canvas.