Programmatically change the Stretch and StretchDirection of content within a Viewbox. : Viewbox « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » Viewbox 
24.65.1.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.";
        }
    }
}
WPF Programmatically Change The Stretch And Stretch Direction Of Content Within A Viewbox
24.65.Viewbox
24.65.1.Programmatically change the Stretch and StretchDirection of content within a Viewbox.Programmatically change the Stretch and StretchDirection of content within a Viewbox.
24.65.2.Create an instance of Viewbox in code. Use Viewbox to stretch or scale a single child element.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.