Change StackPanel Orientation


   
  


<Window x:Class="LayoutPanels.SimpleStack"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SimpleStack" Height="223" Width="354" MinWidth="50">
  <StackPanel Margin="3" Name="stackPanel1">
    <Label Margin="3" HorizontalAlignment="Center">
      A Button Stack
    </Label>
    <Button Margin="3" MaxWidth="200" MinWidth="100">Button 1</Button>
    <Button Margin="3" MaxWidth="200" MinWidth="100">Button 2</Button>
    <Button Margin="3" MaxWidth="200" MinWidth="100">Button 3</Button>
    <Button Margin="3" MaxWidth="200" MinWidth="100">Button 4</Button>

    <CheckBox Name="chkVertical" Margin="10" HorizontalAlignment="Center"
     Checked="chkVertical_Checked" Unchecked="chkVertical_Unchecked">
      Use Vertical Orientation</CheckBox>            
  </StackPanel>
</Window>



//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LayoutPanels
{
    public partial class SimpleStack : Window
    {

        public SimpleStack()
        {
            InitializeComponent();
        }

        private void chkVertical_Checked(object sender, RoutedEventArgs e)
        {
            stackPanel1.Orientation = Orientation.Horizontal;
        }

        private void chkVertical_Unchecked(object sender, RoutedEventArgs e)
        {
            stackPanel1.Orientation = Orientation.Vertical;
        }
    }
}

   
    
     


Set height, Background and Orientation for StackPanel


   
             

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  HorizontalAlignment="Center" VerticalAlignment="Center">

<StackPanel Height="100" Background="Yellow" Orientation="Horizontal">
  <Button>Foo</Button>
  <Button Height="30">Bar</Button>
  <Button Height="195">Quux</Button>
</StackPanel>

</Page>

   
    
    
    
    
    
    
    
    
    
    
    
    
     


Simple StackPanel


   
    

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Fun with Panels!" Height="313" Width="532">
  <StackPanel Background="LightSteelBlue" Orientation ="Horizontal">
    <Label Name="lblInstruction" FontSize="15">Enter Car Information</Label>
    <Label Name="lblMake">Make</Label>
    <TextBox Name="txtMake"/>
    <Label Name="lblColor">Color</Label>
    <TextBox Name="txtColor"/>
    <Label Name="lblPetName">Pet Name</Label>
    <TextBox Name="txtPetName"/>
    <Button Name="btnOK">OK</Button>
  </StackPanel>
</Window>

   
    
    
    
     


StackPanel with StackPanel


   
    


<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      WindowTitle="StackPanel Sample">

    <StackPanel>
        <TabControl MinHeight="500" MinWidth="400">
            <TabItem Header="StackPanel" IsSelected="true">
                <StackPanel>
                    <Border Background="Red" BorderBrush="Black" BorderThickness="1">
                        <TextBlock Foreground="black" FontSize="12">Stacked Item #1</TextBlock>
                    </Border>
                    <Border Width="400" Background="Blue" BorderBrush="Black" BorderThickness="1">
                        <TextBlock Foreground="black" FontSize="14">Stacked Item #2</TextBlock>
                    </Border>
                    <Border Background="Yellow" BorderBrush="Black" BorderThickness="1">
                        <TextBlock Foreground="black" FontSize="16">Stacked Item #3</TextBlock>
                    </Border>
                    <Border Width="200" Background="Green" BorderBrush="Black" BorderThickness="1">
                        <TextBlock Foreground="black" FontSize="18">Stacked Item #4</TextBlock>
                    </Border>
                    <Border Background="White" BorderBrush="Black" BorderThickness="1">
                        <TextBlock Foreground="black" FontSize="20">Stacked Item #5</TextBlock>
                    </Border>
                </StackPanel>
            </TabItem>
        </TabControl>

    </StackPanel>

</Page>

   
    
    
    
     


Stretch = None


   
    

<Window x:Class="Chapter05.ImageBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Chapter05" Height="300" Width="300">
      <StackPanel Margin="5" Grid.Column="0" Grid.Row="0">
        <TextBlock Margin="5" Text="Stretch = None" />
        <Button Width="135" Height="100">
          <Button.Background>
            <ImageBrush ImageSource="c:image.jpg" Stretch="None" />
          </Button.Background>
        </Button>
      </StackPanel>
</Window>

   
    
    
    
     


Stretch = Fill


   
    

<Window x:Class="Chapter05.ImageBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Chapter05" Height="300" Width="300">
      <StackPanel Margin="5" Grid.Column="1" Grid.Row="0">
        <TextBlock Margin="5" Text="Stretch = Fill" />
        <Button Width="135" Height="100">
          <Button.Background>
            <ImageBrush ImageSource="c:image.jpg" Stretch="Fill" />
          </Button.Background>
        </Button>
      </StackPanel>
</Window>

   
    
    
    
     


Stretch = UniformToFill

   
    

<Window x:Class="Chapter05.ImageBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Chapter05" Height="300" Width="300">
      <StackPanel Margin="5" Grid.Column="1" Grid.Row="1">
        <TextBlock Margin="5" Text="Stretch = UniformToFill" />
        <Button Width="135" Height="100">
          <Button.Background>
            <ImageBrush ImageSource="c:image.jpg" Stretch="UniformToFill" />
          </Button.Background>
        </Button>
      </StackPanel>
</Window>