0

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?

Idanis
  • 1,918
  • 6
  • 38
  • 69

1 Answers1

0

Do you wait until your view is fully loaded? If you try to access these properties to early they might not be initialized yet.

Another answer (Why are ActualWidth and ActualHeight 0.0 in this case?) suggests to call

window.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
window.Arrange(new Rect(0, 0, window.DesiredWidth, window.DesiredHeight));

measure and arrange which forces a recalculation of the dimensions of your objects in your view.

Community
  • 1
  • 1
Felix
  • 81
  • 2