Solved

How to dynamically add checkboxes to a Stack Panel

Posted on 2012-04-02
1
914 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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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

19 Experts available now in Live!

Get 1:1 Help Now