The following code sampple creates a Viewbox in C#. Create a WPF app in Visual Studio and copy this method and call it in the constructor of MainWindow.
- private void CreateViewboxDynamically()
- {
- // Create a Viewbox object
- Viewbox dynamicViewbox = new Viewbox();
- // Set StretchDirection and Stretch properties
- dynamicViewbox.StretchDirection = StretchDirection.Both;
- dynamicViewbox.Stretch = Stretch.Fill;
- dynamicViewbox.MaxWidth = 300;
- dynamicViewbox.MaxHeight = 200;
-
- // Create an Ellipse dynamically
- Ellipse redCircle = new Ellipse();
- redCircle.Height = 100;
- redCircle.Width = 100;
- redCircle.Fill = new SolidColorBrush(Colors.Red);
-
- // Set Viewbox.Child to Ellipse
- dynamicViewbox.Child = redCircle;
-
- // Add Viewbox to Grid panel's child
- RootLayout.Children.Add(dynamicViewbox);
- }