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>

   
    
    
    
     


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>

   
    
    
    
     


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>

   
    
    
    
     


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>

   
    
    
    
    
    
    
    
    
    
    
    
    
     


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;
        }
    }
}

   
    
     


Use the methods that are defined by the IScrollInfo interface to scroll the child content of a StackPanel.


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ScrollViewer_Methods.Window1"
    Title="ScrollViewer IScrollInfo Sample"
    Loaded="onLoad">
<DockPanel>
<TextBlock DockPanel.Dock="Top" FontSize="20" FontWeight="Bold" Margin="10">IScrollInfo Interface Methods</TextBlock>
<StackPanel DockPanel.Dock="Left" Width="150">
    <Button Click="spLineUp">Adjust Line Up</Button>
    <Button Click="spLineDown">Adjust Line Down</Button>
    <Button Click="spLineRight">Adjust Line Right</Button>
    <Button Click="spLineLeft">Adjust Line Left</Button>
    <Button Click="spPageUp">Adjust Page Up</Button>
    <Button Click="spPageDown">Adjust Page Down</Button>
    <Button Click="spPageRight">Adjust Page Right</Button>
    <Button Click="spPageLeft">Adjust Page Left</Button>
</StackPanel>  
<Border BorderBrush="Black" Background="White" BorderThickness="2" Width="500" Height="500">
    <ScrollViewer Name="sv1" CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
        <StackPanel Name="sp1">
            <Rectangle Width="700" Height="500" Fill="Green"/>
            <TextBlock>Rectangle 3</TextBlock>
        </StackPanel> 
    </ScrollViewer>
</Border>
</DockPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Text;

namespace ScrollViewer_Methods
{
    public partial class Window1 : Window
    {
        private void onLoad(object sender, System.EventArgs e)
        {
            ((IScrollInfo)sp1).CanVerticallyScroll = true;
            ((IScrollInfo)sp1).CanHorizontallyScroll = true;
            ((IScrollInfo)sp1).ScrollOwner = sv1;
        }
        private void spLineUp(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).LineUp();
        }
        private void spLineDown(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).LineDown();
        }
        private void spLineRight(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).LineRight();
        }
        private void spLineLeft(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).LineLeft();
        }
        private void spPageUp(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).PageUp();
        }
        private void spPageDown(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).PageDown();
        }
        private void spPageRight(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).PageRight();
        }
        private void spPageLeft(object sender, RoutedEventArgs e)
        {
            ((IScrollInfo)sp1).PageLeft();
        }
    }
}

   
    
     


Use StackPanel to arrange child objects in a single line that you can align horizontally or vertically.


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="StackPanel_layout.Window1"
    Title="StackPanel Sample">
  <Border BorderBrush="Black" Background="White" BorderThickness="2">
        <Grid VerticalAlignment="Top" HorizontalAlignment="Left">
          <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition Height="400"/>
          </Grid.RowDefinitions>
                <TextBlock Grid.Row="2" Grid.Column="0">Change StackPanel Orientation:</TextBlock>
                <TextBlock Grid.Row="2" Grid.Column="2">Change HorizontalAlignment:</TextBlock>
                <TextBlock Grid.Row="2" Grid.Column="4">Change VerticalAlignment:</TextBlock>
                <ListBox VerticalAlignment="Top" SelectionChanged="changeOrientation" Grid.Row="2" Grid.Column="1" Width="100" Height="50" Margin="0,0,0,10">
                    <ListBoxItem>Horizontal</ListBoxItem>
                    <ListBoxItem>Vertical</ListBoxItem>
                </ListBox>
                <ListBox VerticalAlignment="Top" SelectionChanged="changeHorAlign" Grid.Row="2" Grid.Column="3" Width="100" Height="50" Margin="0,0,0,10">
                    <ListBoxItem>Left</ListBoxItem>
                    <ListBoxItem>Right</ListBoxItem>
                    <ListBoxItem>Center</ListBoxItem>
                    <ListBoxItem>Stretch</ListBoxItem>
                </ListBox>
                <ListBox VerticalAlignment="Top" SelectionChanged="changeVertAlign" Grid.Row="2" Grid.Column="5" Width="100" Height="50" Margin="0,0,0,10">
                    <ListBoxItem>Top</ListBoxItem>
                    <ListBoxItem>Bottom</ListBoxItem>
                    <ListBoxItem>Center</ListBoxItem>
                    <ListBoxItem>Stretch</ListBoxItem>
                </ListBox>
          <StackPanel Grid.ColumnSpan="6" Grid.Row="3" Name="sp1" Background="Yellow">
                <Button>One</Button>
                <Button>Two</Button>
                <Button>Three</Button>
                <Button>Four</Button>
                <Button>Five</Button>
                <Button>Six</Button>
          </StackPanel>
        </Grid>
  </Border>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace StackPanel_layout
{
  public partial class Window1 : Window
  {

        public void changeOrientation(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
            if (li.Content.ToString() == "Horizontal")
            {
        sp1.Orientation = System.Windows.Controls.Orientation.Horizontal;
            }
            else if (li.Content.ToString() == "Vertical")
            {
        sp1.Orientation = System.Windows.Controls.Orientation.Vertical;
            }
        }
        public void changeHorAlign(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
            if (li.Content.ToString() == "Left")
            {
        sp1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            }
            else if (li.Content.ToString() == "Right")
            {
        sp1.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            }
            else if (li.Content.ToString() == "Center")
            {
        sp1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            }
            else if (li.Content.ToString() == "Stretch")
            {
        sp1.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            }
        }

        public void changeVertAlign(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
            if (li.Content.ToString() == "Top")
            {
        sp1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            }
            else if (li.Content.ToString() == "Bottom")
            {
        sp1.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
            }
            else if (li.Content.ToString() == "Center")
            {
        sp1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            }
            else if (li.Content.ToString() == "Stretch")
            {
        sp1.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            }
        }
        
    }
}