Programmatically change the Stretch and StretchDirection of content within a Viewbox.


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Viewbox_Stretch_Layout_Samp.Window1"
    Title="ViewBox Stretch and StretchDirection Sample">

  <DockPanel Background="White">
        <StackPanel HorizontalAlignment="Center" Orientation="Horizontal" DockPanel.Dock="Top">
            <Button Name="btn1" Click="stretchNone">Stretch="None"</Button>
            <Button Name="btn2" Click="stretchFill">Stretch="Fill"</Button>
            <Button Name="btn3" Click="stretchUni">Stretch="Uniform"</Button>
            <Button Name="btn4" Click="stretchUniFill">Stretch="UniformToFill"</Button>
            <Button Name="btn5" Click="sdUpOnly">StretchDirection="UpOnly"</Button>
            <Button Name="btn6" Click="sdDownOnly">StretchDirection="DownOnly"</Button>
            <Button Name="btn7" Click="sdBoth">StretchDirection="Both"</Button>
         </StackPanel>        
        
        <TextBlock DockPanel.Dock="Top" Name="txt1" />
        <TextBlock DockPanel.Dock="Top" Name="txt2" />
        
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <Viewbox MaxWidth="500" MaxHeight="500" Name="vb1">
                <Image Source="c:image.jpg"/>
            </Viewbox>    
        </StackPanel>
  </DockPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Viewbox_Stretch_Layout_Samp
{
  public partial class Window1 : Window
  {
        public void stretchNone(object sender, RoutedEventArgs e)
        {
            vb1.Stretch = System.Windows.Media.Stretch.None;
            txt1.Text = "Stretch is now set to None.";
        }

        public void stretchFill(object sender, RoutedEventArgs e)
        {
            vb1.Stretch = System.Windows.Media.Stretch.Fill;
            txt1.Text = "Stretch is now set to Fill.";
        }

        public void stretchUni(object sender, RoutedEventArgs e)
        {
            vb1.Stretch = System.Windows.Media.Stretch.Uniform;
            txt1.Text = "Stretch is now set to Uniform.";
        }

        public void stretchUniFill(object sender, RoutedEventArgs e)
        {
            vb1.Stretch = System.Windows.Media.Stretch.UniformToFill;
            txt1.Text = "Stretch is now set to UniformToFill.";
        }

        public void sdUpOnly(object sender, RoutedEventArgs e)
        {
            vb1.StretchDirection = System.Windows.Controls.StretchDirection.UpOnly;
            txt2.Text = "StretchDirection is now UpOnly.";
        }

        public void sdDownOnly(object sender, RoutedEventArgs e)
        {
            vb1.StretchDirection = System.Windows.Controls.StretchDirection.DownOnly;
            txt2.Text = "StretchDirection is now DownOnly.";
        }

        public void sdBoth(object sender, RoutedEventArgs e)
        {
            vb1.StretchDirection = System.Windows.Controls.StretchDirection.Both;
            txt2.Text = "StretchDirection is now Both.";
        }
    }
}

   
    
     


Normal Slider (Max=100, Val=10)


   
     

<Window x:Class="ClassicControls.SlidersCompared"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SlidersCompared" Height="300" Width="400">
    <Grid>
      <StackPanel Margin="10">
        <TextBlock Margin="0,0,0,5">Normal Slider (Max=100, Val=10)</TextBlock>
        <Slider Maximum="100" Value="10"></Slider>

      </StackPanel>
    </Grid>
</Window>

   
    
    
    
    
     


A Delay of 0 and Interval of 1 make this a fast slider.


   
     

<Window x:Class="ClassicControls.SliderTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SliderTest" Height="252" Width="482"
     >
  <StackPanel>
    <TextBlock Margin="10" TextWrapping="Wrap">
      
    </TextBlock>
    
    <Slider Margin="10" TickFrequency="1" TickPlacement="TopLeft" 
            Maximum="10" Delay="0" Interval="1"
          >
    </Slider>
   
    </StackPanel>
    
  
</Window>

   
    
    
    
    
     


Shape Exclude


   
     
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Microsoft.Samples.Graphics.RectangleExample"
    Title="Example">
  <Window.Resources>
    <RectangleGeometry x:Key="rect" Rect="0 0 100 100"></RectangleGeometry>
    <EllipseGeometry x:Key="ellipse" Center="85 50" RadiusX="65" RadiusY="35"></EllipseGeometry>
  </Window.Resources>    
  <Canvas>
      <Path Grid.Row="3" Fill="Yellow" Stroke="Blue" Margin="5">
        <Path.Data>
          <CombinedGeometry GeometryCombineMode="Exclude"
            CombinedGeometry.Geometry1="{StaticResource rect}"
            CombinedGeometry.Geometry2="{StaticResource ellipse}">
          </CombinedGeometry>                  
        </Path.Data>
      </Path>
      <TextBlock Margin="10" VerticalAlignment="Center">Exclude</TextBlock>
      

  </Canvas>
</Window>

   
    
    
    
    
     


Shape Xor


   
     
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Microsoft.Samples.Graphics.RectangleExample"
    Title="Example">
  <Window.Resources>
    <RectangleGeometry x:Key="rect" Rect="0 0 100 100"></RectangleGeometry>
    <EllipseGeometry x:Key="ellipse" Center="85 50" RadiusX="65" RadiusY="35"></EllipseGeometry>
  </Window.Resources>    
  <Canvas>
      <Path Fill="Yellow" Stroke="Blue" Margin="5">
        <Path.Data>
          <CombinedGeometry GeometryCombineMode="Xor"
            CombinedGeometry.Geometry1="{StaticResource rect}"
            CombinedGeometry.Geometry2="{StaticResource ellipse}">
          </CombinedGeometry>
        </Path.Data>
      </Path>
      <TextBlock Margin="10" VerticalAlignment="Center">Xor</TextBlock>

  </Canvas>
</Window>

   
    
    
    
    
     


Shape Union


   
     
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Microsoft.Samples.Graphics.RectangleExample"
    Title="Example">
    <Window.Resources>
        <RectangleGeometry x:Key="rect" Rect="0 0 100 100"></RectangleGeometry>
        <EllipseGeometry x:Key="ellipse" Center="85 50" RadiusX="65" RadiusY="35"></EllipseGeometry>
    </Window.Resources>
    <Canvas>
        <Path Fill="Yellow" Stroke="Blue" Margin="5">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Union"
            CombinedGeometry.Geometry1="{StaticResource rect}"
            CombinedGeometry.Geometry2="{StaticResource ellipse}">
                </CombinedGeometry>
            </Path.Data>
        </Path>
        <TextBlock Margin="10" VerticalAlignment="Center">Union</TextBlock>

    </Canvas>
</Window>

   
    
    
    
    
     


Multicolored Gradient


   
     

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Microsoft.Samples.Graphics.RectangleExample"
    WindowTitle="Example">
  <Canvas>
    <Rectangle Width="150" Height="100" Grid.Row="4" Margin="5">
      <Rectangle.Fill>
        <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
          <GradientStop Color="Yellow" Offset="0.0" />
          <GradientStop Color="Red" Offset="0.25" />
          <GradientStop Color="Blue" Offset="0.75" />
          <GradientStop Color="LimeGreen" Offset="1.0" />
        </LinearGradientBrush>
      </Rectangle.Fill>
    </Rectangle>
    <TextBlock Grid.Row="4" Grid.Column="1" VerticalAlignment="Center" Margin="5">Multicolored Gradient</TextBlock>


  </Canvas>
</Page>