Home  separator  Layout  separator  ViewBox
Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  


How to use the ViewBox in WPF

Introduction

The ViewBox is a very useful control in WPF. If does nothing more than scale to fit the content to the available size. It does not resize the content, but it transforms it. This means that also all text sizes and line widths were scaled. Its about the same behavior as if you set the Stretch property on an Image or Path to Uniform.

Although it can be used to fit any type of control, it's often used for 2D graphics, or to fit a scalable part of a user interface into an screen area.

<Button Content="Test" />
 
<Viewbox Stretch="Uniform">
    <Button Content="Test" />
</Viewbox>
 




Last modified: 2009-05-19 00:16:31
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Ujval
Commented on 17.July 2009
Textwrapping is not working with Viewbox.. I tried lots of.. please let me know if any solution or alternative
Christian Moser
Commented on 17.July 2009
Hi Ujval,
The viewbox gives it's content infinite space. Then is measures the size of the content and scales the content, so that is fits into box. That is the reason why textwarpping is not working, because the TextBlock has no reason to wrap the text, if infinite space is available.
vimal
Commented on 9.August 2009
Hi Moser ,

I have a problem using the viewbox control. I have a shape derived from frameworkelement. I am getting these shapes dynamically. The dimension of these shapes is very small. What i need to do is to show these shapes to a fixed size enlarged . say (width = 50, height = 50) . How can i do this. What i did is , I made this shape inside a border set it a width and height. Then i put these inside a viewbox and made stretch = fill . But it didnt work as i expected. The shape goes beyond 50 and a lot more.. pls help.
vimal
Commented on 25.August 2009
I fixed it eventhough you didnt tell me a solution
Murad Mohd Zain
Commented on 5.November 2009
How you did it Vimal?
Raul
Commented on 18.November 2009
How can i set the background color of a VeiwBox? There is no property available for it :|
Murad Mohd Zain
Commented on 19.January 2010
vimal I like your solution. Thanks.
James
Commented on 21.January 2010
This is way too simplistic to actually be defined as a tutorial on the viewbox. Where is the data binding to the viewbox, the many properties associated with the viewbox and the databinding to those properties. How do you change the view within the viewbox, lets say that little button was a separate user control with it's own code behind containing it's own event handlers and you clicked it and the intent was to send a larger version of the button, scaled to not look like a brick from super mario bros and display it in the viewbox. Not only that what if it had an enumeration of buttons and then you wanted to display them in the viewbox like a dynamic picture viewer. Way to do the bare minimum and stop, you could of at least provided a link that says, for more go to...
Jean-Pierre...
Commented on 29.January 2010
Great tutorial. That's all I needed. Thanks
Betsy
Commented on 12.June 2010
I am making a image viewer. First I have Buttons inside a uniforgrid, I want to click the Button and send the image to a viewbox. Can you help me with this? Thanks for all the tutorials,they are very helpful!!!
vimal
Commented on 24.June 2010
hi betsy,
If i get u right, then u can put a contentcontrol inside your viewbox and set the image as the content of the content control. Hope this is what u need.
vimal
Commented on 24.June 2010
hi betsy,
If i get u right, then u can put a contentcontrol inside your viewbox and set the image as the content of the content control. Hope this is what u need.
Betsy
Commented on 27.June 2010
Thank-you vimal I'll work on that.
C-MOSE
Commented on 23.August 2010
I apprieciate your feedback but I cant explain it anymore in depth because I just copy and pasted this from another website
SHeeDeED
Commented on 6.January 2011
Hi, Can the viewbox tool display Pictures? and if the answer is yes, How to do so?
...
Commented on 10.February 2011
DragonBaki
Commented on 7.April 2011
Can anybody tell me that why i cant set the value from code behind for the child element of Viewbox. For eg., I have the structure like this..

&lt;Border&gt;
&lt;ViewBox&gt;
&lt;TextBlock/&gt;
&lt;/ViewBox&gt;
&lt;/Border&gt;

In this structure, I need to display the textblock after animation. I cant able to this by making the visibility property as visible from code behind as well as i cant able to set the text value from code behind.. It shows the error as &quot;Object reference not set&quot;. Can anybody help me out to overcome it..?
dfghsdfhsd
Commented on 16.April 2011
dfsgbdfsgdf
Bob Dole
Commented on 24.May 2011
Hi How are you?
Velvet
Commented on 8.June 2011
That\'s way the besetst answer so far!
boom
Commented on 8.June 2011
i m fine
Prathyusha
Commented on 20.August 2011
hi,

can we display viewbox as message box

Name
E-Mail (optional)
Comment