DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Multi-column StackPanel for Windows Phone 7

Jevgeni Tšaikin user avatar by
Jevgeni Tšaikin
·
Nov. 27, 11 · Interview
Like (0)
Save
Tweet
Share
7.86K 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

  • 5 Software Developer Competencies: How To Recognize a Good Programmer
  • 11 Observability Tools You Should Know
  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • Monolithic First

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: