Windows Phone Toolkit ExpanderView in depth| Part1: key concepts and API
published on: 9/6/2011 | Views: N/A | Tags: WP7Toolkit Mango
by WindowsPhoneGeek
One of the new components which comes with the latest update of the Windows Phone Toolkit - August 2011 (7.1 SDK) is ExpanderView. Basically, it is some kind of expander control which displays a header and a collapsible drop-down. When the user taps the header, the ExpanderView is expanded / collapsed. ExpanderView is in itself a simple HeaderedItemsControl that exposes various additional properties and events for controlling the expanding and collapsing functionality.
I will continue our "Toolkit in Depth" series with two more posts that cover all about the Windows Phone ExpanderView control in details. In Part1 I am going to talk about key properties, methods, events and the main features of the Windows Phone ExpanderView control.
- Windows Phone Toolkit ExpanderView in depth| Part1: key concepts and API
- Windows Phone Toolkit ExpanderView in depth| Part2: databinding
Getting Started
To begin using ExpanderView, first add a reference to the Microsoft.Phone.Controls.Toolkit.dll assembly in your Windows Phone application project.
NOTE: Microsoft.Phone.Controls.Toolkit.dll assembly is installed with the toolkit and you can find it in:
For 32-bit systems:
C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Toolkit\Aug11\Bin\Microsoft.Phone.Controls.Toolkit.dll
For 64-bit systems:
C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Toolkit\Aug11\Bin\Microsoft.Phone.Controls.Toolkit.dll
Alternatively you can select it directly from the "...\Silverlight for Windows Phone Toolkit Source & Sample - Aug 2011\Bin\" if you have downloaded the "Silverlight for Windows Phone Toolkit Source & Sample - Aug 2011.zip" instead.
NOTE: If you have installed the toolkit via NuGet then the reference is automatically added to your project.
You can create an instance of the ExpanderView control either in XAML or with C#/VB.
- Define an instance of the ExpanderView control in XAML: you have to add the following namespace declaration:
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
<toolkit:ExpanderView Header="Expander Header" x:Name="expander">
<toolkit:ExpanderView.Items>
<TextBlock FontSize="40" Text="Expanded Content"/>
</toolkit:ExpanderView.Items>
</toolkit:ExpanderView>
NOTE: Make sure that your page declaration includes the "toolkit" namespace! You can do this by using VisualStudio Toolbox, Expression Blend Designer or just include it on your own.
- Define an instance of the ExpanderView control in C#: just add the following using directive:
using Microsoft.Phone.Controls;
ExpanderView expander = new ExpanderView();
expander.Header = "Expander Header";
expander.Items.Add(new TextBlock() {Text = "Expanded Content"});
Key Properties
-
Expander
Expander is a dependency property of type object. It gets or sets the expander object.
Example:
<toolkit:ExpanderView Header="Expander Header" x:Name="expander">
<toolkit:ExpanderView.Items>
<TextBlock FontSize="40" Text="Expanded Content 1" Foreground="Orange"/>
<TextBlock FontSize="40" Text="Expanded Content 2" Foreground="YellowGreen"/>
</toolkit:ExpanderView.Items>
<toolkit:ExpanderView.Expander>
<Rectangle Height="100" Width="400" Fill="Green"/>
</toolkit:ExpanderView.Expander>
</toolkit:ExpanderView>
- ExpanderTemplate
Expander is a dependency property of type DataTemplate. It gets or sets the template used to display the control's expander.
- IsNonExpandable
IsNonExpandable is a dependency property of type bool. It gets or sets the flag that indicates whether the Expander View is non-expandable.
- NonExpandableHeader
NonExpandableHeader is a dependency property of type object. It gets or sets the header object that is displayed when the Expander View is non-expandable.
Example:
<toolkit:ExpanderView Header="Expander Header" x:Name="expander" NonExpandableHeader="Non Expander Header" IsNonExpandable="True">
<toolkit:ExpanderView.Items>
<TextBlock FontSize="40" Text="Expanded Content"/>
</toolkit:ExpanderView.Items>
</toolkit:ExpanderView>
- NonExpandableHeaderTemplate
NonExpandableHeaderTemplate is a dependency property of type DataTemplate. It gets or sets the data template that defines the non-expandable header.
- IsExpanded
IsExpanded is a dependency property of type bool. It gets or sets the flag that indicates whether the Expander View is expanded.
- HasItems
HasItems is a dependency property of type bool. It gets or sets the flag that indicates whether the Expander View has items.
Key Events
- Expanded
Occurs when the ExpanderView expands to display its content.
Example:
expander.Expanded += new System.Windows.RoutedEventHandler(expander_Expanded);
//...
void expander_Expanded(object sender, System.Windows.RoutedEventArgs e)
{
//...
}
- Collapsed
Occurs when the ExpanderView collapses to hide its content.
Example:
expander.Collapsed += new System.Windows.RoutedEventHandler(expander_Collapsed);
//...
void expander_Collapsed(object sender, System.Windows.RoutedEventArgs e)
{
//...
}
That was all about the basic usage of ExpanderView from the Windows Phone Toolkit - August 2011 (7.1 SDK) in depth. In the next post I will talk about using the ExpanderView with data binding, so stay tuned.
The source code is available here:
You may also find interesting the following articles:
- Windows Phone Toolkit LockablePivot in depth
- Windows Phone Toolkit PhoneTextBox in depth
- Windows Phone HubTile in depth| Part1: key concepts and API
- Windows Phone HubTile in depth| Part2: Data Binding
- 21 WP7 Toolkit in Depth articles covering all controls
You can also follow us on Twitter @winphonegeek
Comments
Expander
posted by: Elena Kihn on 9/6/2011 4:16:32 PM
Nice to see that we now have Expander for Windows Phone. It seems that the toolkit really offers some good components at all.
line through the header?
posted by: Praschi on 10/7/2011 7:47:33 PM
When I use this control, it looks not very good because the line on the left (left to the expanded items) is drawn through the header. Do you know how to fix that?
posted by: Geoff on 12/7/2011 1:24:19 PM
I had the same problem with the line, fix here:
http://geoffwebbercross.blogspot.com/2011/12/windows-phone-toolkit-expander-template.html
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
