[英]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.
我希望自动生成stackpanels。
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. 例如,如果5个用户在ui中输入他/她的详细信息并单击添加按钮,则应为每个用户自动生成带有用户信息的5个stackpanel。 Please what wpfcontrol can I use for this functionality?
请问我可以使用哪些wpfcontrol来实现此功能? 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. 您可以创建一个包含StackPanel及其内容的UserControl。 You would then be able to instantiate that UserControl on-the-fly whenever you want it.
然后,您可以随时实时地实例化UserControl。 See Creating & using a UserControl for a tutorial on WPF UserControl.
有关WPF UserControl的教程,请参阅创建和使用 UserControl。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.