开发者

How to retrieve Canvas dimensions in the viewmodel

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&开发者_运维知识库gt;
</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 PassEventArgsToCommand that when set to True sends the event args to the appropriate RelayCommand in the viewmodel. So in the viewmodel, I initialised the command thusly:

Commands.ResizeCommand = new RelayCommand<SizeChangedEventArgs>(action => RecalculateObjectPositions(action));

and in the RecalculateObjectPositions method, I can access e.NewSize to find the new size of the canvas.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜