Why the Canvas size is set to 0 (zero) after making it a children of a StackPanel defined in XAML?
-
Test1
-
Create a canvas, set its size and then add it to the StackPanelDefinedInXAML.
-
After making it a child of the StackPanelDefinedInXAML, canvas.ActualWidth=0 and canvas.ActualHeight=0;
-
Test2
-
Create a canvas, set its size and then add it to a StackPanelDefinedInCode
-
After making it a child of the StackPanelDefinedInCode, canvas.ActualWidth=5000 and canvas.ActualHeight=5000;
Why Test1 resets the size of the canvas and Test2 doesn’t reset the size of the canvas?
<ScrollViewer Name="Scroller">
<StackPanel Name="PanelDefinedInXAML" />
</ScrollViewer>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
FirstPage.Loaded += FirstPage_Loaded;
}
private void FirstPage_Loaded(object sender, RoutedEventArgs e)
{
Test1();
Test2();
}
private void Test1()
{
Canvas canvas = new Canvas();
canvas.Width = 5000;
canvas.Height = 5000;
PanelDefinedInXAML.Children.Add(canvas);
}
private void Test2()
{
Canvas canvas = new Canvas();
canvas.Width = 5000;
canvas.Height = 5000;
StackPanel panelDefinedInCode = new StackPanel();
Scroller.Content = panelDefinedInCode;
panelDefinedInCode.Children.Add(canvas);
}