13

Is it possible to have a <StackPanel> or labels (or buttons, whatever) going horizontally across it's parent control (e.g. Window) and have it fill out all the space allowed?

For example if I had 3 controls

_ window width__

[ 1 ]__ [ 2 ] __ [ 3 ]

or 2 controls

_ window width__

[ 1 ]_______ [ 2 ]

in each case the full width of the window is being utilized with each edge control being left and right justified respectively.

Ternary
  • 2,401
  • 3
  • 31
  • 54

2 Answers2

30

A StackPanel will stack controls, so no is the short answer. That's not what a StackPanel is designed to do.

You could use a grid as Vlad suggested. Something like this:

    <Grid HorizontalAlignment="Stretch">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <Button Width="20" Height="20" Grid.Column="0"/>
        <Button Width="20" Height="20" Grid.Column="2"/>
        <Button Width="20" Height="20" Grid.Column="4"/>
    </Grid>
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • 1
    So then do I need to dynamically manage adding a column for each new entry and placing each control into it's own cell? – Ternary Mar 28 '12 at 18:20
  • 1
    In the case of displaying a dynamic collection of items, you might want to think about using the ListBox control and set it's ItemPanel to a UniformGrid. That might get close to what you want, but I don't think you'll get the left and right justification on the first and last item. – Matt Burland Mar 28 '12 at 18:37
-6
        StackPanel m_pstackpanel = (StackPanel)this.Parent;
        m_pstackpanel.Children.Clear();
        keypadclasslist keypadWindow = new keypadclasslist(m_pLstReceiver);
        m_pstackpanel.Children.Add(keypadWindow);