Solved

How to dynamically add checkboxes to a Stack Panel

Posted on 2012-04-02
1
919 Views
Last Modified: 2012-04-05
I have a stack panel and would like to dynamically add check boxes based on a list of items I get from a list.

 <StackPanel Grid.Row="5" Height="26"  Orientation="Horizontal" HorizontalAlignment="Stretch" Margin="30,28,0,0" Name="stackPanelPlatform" VerticalAlignment="Stretch" Width="1041"   Loaded="stackPanelPlatform_Loaded">
            <ItemsControl Name="SPPlatforms" ItemsSource="{Binding platforms}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <CheckBox  Content="{Binding Text}" IsChecked="{Binding IsChecked}" Width="200" Name="{Binding Text}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>



Code Behind
private void stackPanelPlatform_Loaded(object sender, RoutedEventArgs e)
        {

            var platforms = new ObservableCollection<Platform>();
            Platform pt = new Platform { Text = "ABC", IsChecked = false };
            platforms.Add(pt);
            pt = new Platform { Text = "XYZ", IsChecked = false };
            platforms.Add(pt);

            SPPlatforms.ItemsSource = platforms;    



        }
0
Comment
Question by:countrymeister
1 Comment
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 37799934
to dynamically add controls to stack panel you should add to the Children collection:
http://msdn.microsoft.com/en-us/library/ms743314.aspx#Y0
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now