Solved

How to dynamically add checkboxes to a Stack Panel

Posted on 2012-04-02
1
924 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

840 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