Search code examples
c#windows-phone-7silverlight-toolkit

How to add checkbox to the scrollviewer by code?


<Grid>
                <ScrollViewer Height="391" HorizontalAlignment="Left" Margin="10,10,0,0" Name="scrollViewer1" VerticalAlignment="Top" Width="427" >
                    <Grid Height="733">

                    </Grid>
                </ScrollViewer>
                <TextBox Height="76" HorizontalAlignment="Left" Margin="19,453,0,0" Name="textBox1" Text="TextBox" VerticalAlignment="Top" Width="254" />
                <Button Content="Button" Height="77" HorizontalAlignment="Left" Margin="290,450,0,0" Name="button1" VerticalAlignment="Top" Width="136" />
            </Grid>

I want to add a checkbox in the scrollviewer when the user clicks the button. A new checkbox should appear below the previous at every click as well. And also, is the ScrollViewer the ideal control for this task?


Solution

  • If you want checkboxes to appear below each other you should use a StackPanel instead of Grid as the container inside the SrollViewer:

       <ScrollViewer Height="391" HorizontalAlignment="Left" Margin="10,10,0,0" Name="scrollViewer1" VerticalAlignment="Top" Width="427" > 
                    <StackPanel Name="CheckBoxContainer">
    
                    </StackPanel> 
       </ScrollViewer> 
    

    And in your click event you add the checkboxes

     CheckBoxContainer.Children.Add(new CheckBox());