简体   繁体   中英

How can I create stackpanels in wpf automatically without using xaml

Newbie here.

I'm writing a code to create information for different users. I want the stackpanels to be generated automatically.

For example if 5 user enters his/her details in the ui and click on the add button, 5 stackpanel with the users info,should be generated automatically for each users. Please what wpfcontrol can I use for this functionality? Thanks all.

        <StackPanel >
            <StackPanel Orientation="Horizontal" Height="32">
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Margin="0,6,5,0"  
                       Text="First Name" VerticalAlignment="Top"/>
                <TextBox Name="firstname" TextWrapping="Wrap" Text="" Margin="0,0,0,9" Width="71"/>

            </StackPanel>

            <StackPanel Orientation="Horizontal" Height="49" RenderTransformOrigin="0.502,1.031">
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Margin="0,6,5,0" 
                       Text="Last Name" VerticalAlignment="Top"/>
                <TextBox Name="lastname" TextWrapping="Wrap" Text="" Margin="0,0,0,26" Width="71" TextChanged="TextBox_TextChanged"/>

            </StackPanel>
        </StackPanel>
        <StackPanel VerticalAlignment="Bottom"  Margin="10,10,0,0" FlowDirection="LeftToRight" Orientation="Horizontal" Grid.Row="1">
            <Button x:Name="Btn_add" Content="Add"  Click="Btn_add_Click"   
                    HorizontalAlignment="Center" VerticalAlignment="Top" Width="44"/>
            <StackPanel Margin="10">

                <WrapPanel Margin="0,10">
                    <TextBlock Text="Full Name: " FontWeight="Bold" />

                    <TextBlock Text="{Binding Path=Text,     ElementName=lastname}" />
                    <TextBlock Text="{Binding Path=Text, ElementName=firstname}" />

                </WrapPanel>
            </StackPanel>
        </StackPanel>


You could create a UserControl that contains your StackPanel and of its content. You would then be able to instantiate that UserControl on-the-fly whenever you want it. See Creating & using a UserControl for a tutorial on WPF UserControl.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM