WPF ViewBox Control
12K views
Nov 7, 2023
WPF ViewBox Control
View Video Transcript
0:00
In this video we are going to see how to create and use a view box control in WPF
0:05
A view box control is used to stretch and scale elements placed on it
0:12
So let's start by creating a new project. For my sample I use Visual Studio 2010
0:20
If you do not have Visual Studio 2010 and using Visual Studio 2008
0:27
You may have to install WPF Toolkit for that. So let's create a new project
0:33
I click on File menu, New Project. Now you want to make sure in the left side, Install Templates, which will see sharp under that Windows
0:44
In the right pane, make sure you have WPF application selected. I'm going to give my application name viewbox sample. Click OK this will create a new project. And as soon as the project is created you will see the designer
1:10
Now, to create viewbox element in XAML creates a viewbox control
1:24
I'm going to give it a name. I'm going to set its width and height to 200 pixels each
1:42
Now I also going to set its vertical alignment and horizontal alignment Vertical alignment to the top and horizontal alignment to the left
2:00
I'm also going to give a little margin. 10 from the top, 10 from the left
2:13
Now, what I am going to do is, let's place an ellipse on this view box and see how it goes
2:24
So I create an ellipse. control by using ellipse element I give it width of ellipse I give 100 and height I give say 50
2:41
I also fill this ellipse with red color so as you can see in this designer my ellipse is created
2:53
by default height and width of this ellipse are 50 and 100 pixels respectively
3:01
Now one thing I'm going to change is in my viewbox code, see this is my viewbox code
3:06
here I'm going to change the stretch property of viewbox, say fill
3:13
Stretch property has four options, it can be fill, it can be none, it can be uniform or uniform to fill
3:23
So as soon as I change my stress property of view box to fill you can see even though my ellipse width is 100 height is 50 pixels but ellipse actual UI shows that it filled in the view box
3:41
So no matter what height or width you set of that ellipse control or any other control placed on view box
3:50
view box actually controls it. So this actually helps when you don't know the actual height and width of the controls placed on a view box
4:00
So this is how you can actually create and use the view box control in Jamal
4:06
Now one thing I'm going to do is I'm going to show how to create a view box control dynamically in your code
4:14
This was just design time. So I'm going to do is let's do one thing
4:19
Let's comment this code right here. and I'm going to give this
4:26
grid name because I'm going to use this in my code behind
4:32
let's say I'll call it root layout now F7 F7 will take you direct to the code behind
4:44
for this window here I'm going to create a method called private. Actually I only have a code so what I'm going to do is I'm going
4:57
to copy my code and we can just explain to save some time. So here I have a method called
5:09
CreateViewBoxDynamically. In this I create a viewbox object using the viewbox class in
5:16
class in WPF and then I set the stretch direction which is both means horizontal and vertical Then I set the stretch to fill I set max width 300 max height 200
5:31
Then I create an ellipse. And then I do set height of ellipse to 100 and width of ellipse to 50
5:41
and I fill with the red color here and then what I do is I said that viewbox.shall to my ellipse
5:53
now viewbox can only have one item on it one element on it so you cannot actually
6:01
place multiple elements on it now in the end what I do is to my root layout I add this dynamic view
6:10
box and now I'm going to call this method after each last components now if
6:21
you click F5 hit F5 to build and run this project let's see what our output
6:27
looks like So this is how my output looks like
6:59
As you can see even though my width and height of the circle ellipse was 150 but it's filled
7:06
in the view box
#Arts & Entertainment
#Computers & Electronics