I have the following code:
Xaml
<GroupBox x:Name="FrameStatusGroupBox" Header="Frame Status" Foreground="DarkRed" Grid.Row="3">
<ItemsControl x:Name="FrameStatusItemsControl" ItemsSource="{Binding KeepFramesSection.FrameStatus, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas x:Name="FrameStatusCanvas" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="LightGray" Height="Auto" Width="Auto" IsEnabled="{Binding MarkingFileLoaded, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</GroupBox>
ViewModel
foreach (var currentCanvas in ListOfCanvases)
{
var widthRatio = FrameStatus.ActualWidth / currentCanvas.ActualWidth;
var heightRatio = FrameStatus.ActualHeight / currentCanvas.ActualHeight;
foreach (var currentBoundingBox in currentCanvas.Children)
{
var savedObj = XamlWriter.Save(currentBoundingBox);
var reader = new StringReader(savedObj);
var xmlReader = XmlReader.Create(reader);
var newChild = (UIElement) XamlReader.Load(xmlReader);
var p = ((UIElement) currentBoundingBox).TransformToAncestor(currentCanvas)
.Transform(new Point(0, 0));
p.X *= widthRatio;
p.Y *= heightRatio;
Canvas.SetLeft(newChild, p.X);
Canvas.SetTop(newChild, p.Y);
FrameStatus.Children.Add(newChild);
}
}
FrameStatus.UpdateLayout();
I'm trying to get the dimensions of both the original Canvas
and the new Canvas
because I want to copy the children from one to the other, and their sizes are different.
When doing that, for some reason, this code: FrameStatus.ActualWidth
and this code: FrameStatus.ActualHeight
return both 0.
What am I doing wrong?