Windows Phone 7 Mango: Using Viewbox control
published on: 5/27/2011 | Views: N/A | Tags: Mango
by WindowsPhoneGeek
In this post I am going to talk about the new WP7 Viewbox control. Viewbox is a well known control in Silverlight 4, so with the recent Windows Phone 7.1(Mango) release it is now available for WP7 as well.
Basically ViewBox takes one child element and automatically stretches or scales it to fit the size of the Viewbox. If does nothing more than scale to fit the content to the available size without resizing it, but transforming it. That is why this control is very helpful when text sizes or other UIElements are scaled.
For now ViewBox control does not appear in the toolbox. To resolve this issue, add the control to your XAML manually, or build it dynamically in code.
Example1: This example shows how to scale text inside TextBlock control in order to to fit the available size. You can see the result with and without Viewbox:
<Viewbox Stretch="UniformToFill" Width="300">
<TextBlock Text="TextBlock Demo with long string text 1." FontSize="25"/>
</Viewbox>
<TextBlock Width="300" Text="TextBlock Demo with long string text 2" FontSize="25"/>
Example2:This example shows how to scale a Button in order to fit the available size. You can see the result with and without Viewbox:
<Viewbox Stretch="Uniform">
<Button Content="ViewBox Test Button"/>
</Viewbox>
<Button Content="NO ViewBox"/>
Example3: This example shows how to stretch content placed inside Grid panel in order to fit the available size. Note that the text is transformed! You can see the result with and without Viewbox:
<Viewbox Stretch="Uniform">
<Grid Width="300">
<Image Source="pic1.jpg"/>
<TextBlock Text="Some Title Here"/>
</Grid>
</Viewbox>
<Grid Width="300">
<Image Source="pic1.jpg"/>
<TextBlock Text="Some Title Here"/>
</Grid>
Example4: This example demonstrates how the Text is scaled when the available size is changes dynamically.
XAML:
<Button Content="Increase Size" Click="btnIncrease_Click"/>
<Button Content="Decrease Size" Click="btnDecrease_Click"/>
<Grid x:Name="resizableGrid" Width="450" Height="450">
<Viewbox Stretch="Uniform">
<TextBlock Text="Some text to be scaled using ViewBox"/>
</Viewbox>
</Grid>
C#:
private void btnIncrease_Click(object sender, RoutedEventArgs e)
{
this.resizableGrid.Width += 50;
this.resizableGrid.Height += 50;
}
private void btnDecrease_Click(object sender, RoutedEventArgs e)
{
this.resizableGrid.Width -= 50;
this.resizableGrid.Height -= 50;
}
NOTE: The WP7 documentation is still not available but you can take a look at the Silverlight MSDN documentation.
That was all about how to use Viewbox in WP7 Mango. Here is the full source code:
I hope that the post was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Interesting
posted by: TP on 5/27/2011 4:50:51 PM
Very interesting, I did not know that there was such control. Thank you guys. Keep up with the good work and keep us up to date.
Help me using Combobox control
posted by: Wp mem on 7/12/2011 7:01:08 PM
Hi ... I was using Combobox control but it not working on WP7.1 Please help me
good post
posted by: ZIS on 7/15/2011 3:23:49 PM
Is there control in WP7.1 to display pdf in arabic format.
Thanks in advance.
RE:WP7.1 to display pdf
posted by: support on 7/18/2011 2:16:26 PM
You can check this post: Displaying PDF Files in Windows Phone 7 Mango
Re:Help me using Combobox control
posted by: winphonegeek on 8/4/2011 11:42:36 PM
Can you give us some more information what exactly is not working in Mango?
Can it work in multiline
posted by: Omar on 9/27/2011 10:41:05 PM
Hi,
Can the ViewBox work on multi line textblock
Regards Omar
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation

