tags:

views:

502

answers:

1

I have the exact same problem as quoted (taken form here , but not answered) :

I add controls to the StackPanel via StackPanel.Childrens.Add( ).

But what i see - all controls, added by me, are in the same position and overlap each other. They don't being layout inside of StackPanel.

Even StackPanel.UpdateLayout( ) brings me nothing.

I for me am trying to add canvasses (yes I do need them) to the stackPanel. Any ideas?

+2  A: 

Have you set explicit sizes on your canvases? Canvases don't size to fit their content, so unless you specify the size of the canvas explicitly, when you put them all in a stack panel, their contents will appear on top of each other, as you explain. This is because the (0,0) position to which canvas children are positioned relative to will be the top-left of the stackpanel (the same point for all canvases and all their children).

Try the following in Kaxaml to highlight the situation:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&gt;
  <StackPanel>
    <Canvas>
      <TextBlock Text="Child of canvas one" />
    </Canvas>
    <Canvas>
      <TextBlock Text="Child of canvas two" />
    </Canvas>
  </StackPanel>
</Page>

You'll see the two lines of text are superimposed on top of one another.

Now, try this:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&gt;
  <StackPanel>
    <Canvas Height="15">
      <TextBlock Text="Child of canvas one" />
    </Canvas>
    <Canvas Height="15">
      <TextBlock Text="Child of canvas two" />
    </Canvas>
  </StackPanel>
</Page>

And you'll see the spacing you desire.

Hope that helps.

Drew Noakes
I had for all the elements inside it, not for, the elsewhere nicely presentating canvas... tx man
Peter
Welcome Peter.
Drew Noakes