Mega Code Archive

 
Categories / C# Tutorial / WPF
 

Create an instance of Viewbox in code Use Viewbox to stretch or scale a single child element

using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; using System.Threading;   public class app : System.Windows.Application   {         System.Windows.Controls.Canvas myCanvas;     System.Windows.Window mainWindow;         System.Windows.Controls.Viewbox myViewbox;         System.Windows.Controls.Grid myGrid;         System.Windows.Controls.TextBlock myTextBlock;         System.Windows.Shapes.Ellipse myEllipse;     protected override void OnStartup (StartupEventArgs e)     {       base.OnStartup (e);       mainWindow = new System.Windows.Window ();       myCanvas = new Canvas();             myCanvas.Background = System.Windows.Media.Brushes.Silver;             myCanvas.Width = 600;             myCanvas.Height = 600;                          myViewbox = new Viewbox();             myViewbox.StretchDirection = StretchDirection.Both;             myViewbox.Stretch = Stretch.Fill;             myViewbox.MaxWidth = 400;             myViewbox.MaxHeight = 400;             myGrid = new Grid();             myEllipse = new Ellipse();             myEllipse.Stroke = Brushes.RoyalBlue;             myEllipse.Fill = Brushes.LightBlue;             myTextBlock = new TextBlock();             myTextBlock.Text = "Viewbox";             myGrid.Children.Add(myEllipse);             myGrid.Children.Add(myTextBlock);             myViewbox.Child = myGrid;                          Canvas.SetTop(myViewbox, 100);             Canvas.SetLeft(myViewbox, 100);             myCanvas.Children.Add(myViewbox);             mainWindow.Content = myCanvas;       mainWindow.Show();     }     [System.STAThread()]     private static void Main ()     {       app app = new app ();       app.Run ();     }   }