DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Multi-column StackPanel for Windows Phone 7

Multi-column StackPanel for Windows Phone 7

Jevgeni Tšaikin user avatar by
Jevgeni Tšaikin
·
Nov. 27, 11 · Mobile Zone · Interview
Like (0)
Save
Tweet
7.67K Views

Join the DZone community and get the full member experience.

Join For Free

Few days ago I have decided to develop multi-column StackPanel control for one of my projects. Basically, I have created many StackPanels in horizontal orientation inside a StackPanel with vertical orientation. It should be a very simple solution and I am going to share it with you in this post. Same solution could be applied to other Silverlight applications.

eugenedotnet multi column stackpanel for Windows Phone 7

I have created a new class MultiColumnStackPanel (check code bellow) that inherit from StackPanel. This new class is using NumberOfColumns property to hold the number of columns for StackPanel. Items property is used to hold UIElement collection. LoadItems method is used to create many single horizontal StackPanels from Items collection. You can add additional handlers to PropertyMetadata of dependency property to handle Items property change events.

public class MultiColumnStackPanel : StackPanel
{
    public int NumberOfColumns { get; set; }
 
    public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items",
            typeof(Collection<UIElement>),
            typeof(MultiColumnStackPanel),
            new PropertyMetadata(new Collection<UIElement>()));
 
    public Collection<UIElement> Items
    {
        get { return (Collection<UIElement>)GetValue(ItemsProperty); }
    }
 
    public MultiColumnStackPanel()
    {
        Orientation = Orientation.Vertical;
        Loaded += (s, e) =>
        {
            LoadItems();
        };
    }
 
    void LoadItems()
    {
        Children.Clear();
        if (Items != null)
        {
            StackPanel sp = CreateNewStackPanel();
            foreach(UIElement item in Items)
            {
                sp.Children.Add(item);
                if (sp.Children.Count == NumberOfColumns)
                {
                    Children.Add(sp);
                    sp = CreateNewStackPanel();
                }
            }
 
            if (sp.Children.Count > 0)
                Children.Add(sp);
        }
    }
 
    private StackPanel CreateNewStackPanel()
    {
        return new StackPanel() { Orientation = Orientation.Horizontal };
    }
}
To test this control I had to add the code bellow to one of XAML pages. Result can be seen on the image above (in the beginning of this post).
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <local:MultiColumnStackPanel x:Name="customStackPanel" NumberOfColumns="2" Margin="0,8,0,-8">
        <local:MultiColumnStackPanel.Items>
            <Rectangle Height="80" Width="200" Margin="10,0,0,0" Fill="Green" />
            <Rectangle Height="80" Width="200" Margin="10,0,0,0" Fill="Green" />
            <Rectangle Height="80" Width="200" Margin="10,10,0,0" Fill="Green" />
            <Rectangle Height="80" Width="200" Margin="10,10,0,0" Fill="Green" />
            <Rectangle Height="80" Width="200" Margin="10,10,0,0" Fill="Green" />
        </local:MultiColumnStackPanel.Items>
    </local:MultiColumnStackPanel>
</Grid>


Source:  http://www.eugenedotnet.com/2011/01/w14-multi-column-stackpanel-for-windows-phone-7/
Windows Phone

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Servlets Listeners Introduction and Examples
  • How to Configure Git in Eclipse IDE
  • Toying With Kotlin’s Context Receivers
  • Federated Schema Design

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo