How to Programmatically Switch between the HubTile Visual States
published on: 10/13/2011 | Views: N/A | Tags: WP7Toolkit
by WindowsPhoneGeek
In this post I am going to talk about how to programmatically switch between different HubTile Visual States. Since the HubTile does not expose any API for changing / switching between its visual states manually it is not obvious how you can do that. However, the solution is pretty simple: you just need to use the VisualStateManager class which manages states and the logic for transitioning between states of controls.
For reference take a look at the official MSDN Documentation.
To begin with, lets first create a new Windows Phone application project and add a reference to Microsoft.Phone.Controls.Toolkit.dll.
NOTE: For more information about the HubTile control take a look at:
- "Silverlight for Windows Phone Toolkit In Depth" FREE e-book
- Windows Phone HubTile in depth| Part1: key concepts and API
Step1. Add the following code in MainPage.xaml:
<phone:PhoneApplicationPage x:Class="HubTileDemo.MainPage" ... xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit">
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Orientation="Vertical">
<toolkit:HubTile x:Name="hubTile" Background="Green" Source="wpglogo.png" Title="Hold Here" Message="This is HubTile message!" Margin="10"/>
<Button x:Name="btnGoToExpanded" Content=" Go To Expanded State" Click="btnGoToExpanded_Click" />
<Button x:Name="btnGoToSemiexpanded" Content="Go To Semiexpanded State" Click="btnGoToSemiexpanded_Click" />
<Button x:Name="btnGoToFlipped" Content="Go To Flipped State" Click="btnGoToFlipped_Click" />
<Button x:Name="btnGoToCollapsed" Content="Go To Collapsed State" Click="btnGoToCollapsed_Click" />
</StackPanel>
Step2. Set the HubTile "IsFrozen" property to true in order to prevent the HubTile Visual State from being changed automatically. Add the following code in MainPage.xaml.cs:
public MainPage()
{
InitializeComponent();
this.hubTile.IsFrozen = true;
}
Step3. Go To Expanded State implementation:
private void btnGoToExpanded_Click(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this.hubTile, "Expanded", true);
}
Step4. Go To Semiexpanded State implementation:
private void btnGoToSemiexpanded_Click(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this.hubTile, "Semiexpanded", true);
}
Step5. Go To Flipped State implementation:
private void btnGoToFlipped_Click(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this.hubTile, "Flipped", true);
}
Step6. Go To Collapsed State implementation:
private void btnGoToCollapsed_Click(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this.hubTile, "Collapsed", true);
}
That was all about programmatically switching between the different HubTile Visual States. The source code is available here:
I hope that the article was helpful.
You may also find interesting the following articles:
- "Silverlight for Windows Phone Toolkit In Depth" FREE e-book
- Working with the Windows Phone HubTile Events
- Windows Phone HubTile in depth| Part1: key concepts and API
- Windows Phone HubTile in depth| Part2: Data Binding
- Windows Phone HubTile in depth| Part3: Freezing and Unfreezing tiles
You can also follow us on Twitter @winphonegeek
Comments
Thanks again
posted by: Steven on 10/15/2011 6:18:40 PM
Thanks again! Very useful for me!
How to Switch on dynamically created HubTiles?
posted by: MSiccDev on 1/30/2012 1:24:46 PM
This works fine with a single HubTile.
But how can I do this if I add my HubTiles as DataTemplate to a ListBox?
The VisualStateManager does not recognize the HubTile as control, and returns alwas null.
Anyone an idea?
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
