我有以下代码:
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>
视图模型
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();
我正在尝试获取原始尺寸Canvas
和新尺寸,Canvas
因为我想将孩子从一个复制到另一个,并且它们的尺寸不同。
这样做时,出于某种原因,此代码:FrameStatus.ActualWidth
和此代码:FrameStatus.ActualHeight
均返回 0。
我究竟做错了什么?