views:

1023

answers:

1

I was wondering if I can have 2 controls in a horizontal-oriented StackPanel so that the right item should be docked to the right side of the StackPanel.

I tried the following but it didn't work:

<StackPanel Orientation="Horizontal">
    <TextBlock>Left</TextBlock>
    <Button Width="30" HorizontalAlignment="Right">Right<Button>
</StackPanel>

In the snippet above I want the Button to be docked to the right side of the StackPanel.

Note: I need it to be done with StackPanel, not Grid etc.

+3  A: 

You can achieve this with a DockPanel:

<DockPanel Width="300">
    <TextBlock>Left</TextBlock>
    <Button HorizontalAlignment="Right">Right</Button>
</DockPanel>

The difference is that a StackPanel will arrange child elements into single line (either vertical or horizontally) whereas a DockPanel defines an area where you can arrange child elements either horizontally or vertically, relative to each other (the Dock property changes the position of an element relative to other elements within the same container. Alignment properties, such as HorizontalAlignment, change the position of an element relative to its parent element).

0xA3
this is what I meant with the 'etc.' I guess the answer is no, and will have to do it the tough way, read my comment for JMD above
Shimmy
At least StackPanel is changed to DockPanel pretty simply. Much simpler than inserting a grid within the StackPanel. +1
JMD
Well, StackPanel simply can't do the layout that you want. A Grid will give you the most flexibility, but a change from StackPanel to DockPanel doesn't seem too difficult.
0xA3