[英]What is the best way to add large number of text box in wpf window
<GroupBox Header="Student Details">
<StackPanel>
<GroupBox>....</GroupBox>
<GroupBox>....</GroupBox>
<GroupBox>....</GroupBox>
</StackPanel>
</GroupBox>
如果列表是固定长度,则将文本框放在另一个容器(如Grid,StackPanel或WrapPanel)中:
<GroupBox Header="Student Details">>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
... as many RowDefinitions as you need
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
... as many ColumnDefinitions as you need
</Grid.ColumnDefinitions>
<GroupBox Grid.Column="0" Grid.Row="0">
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<TextBox .../>
<TextBox .../>
</StackPanel>
</GroupBox>
<GroupBox Grid.Column="0" Grid.Row="1">
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<TextBox .../>
<TextBox .../>
</StackPanel>
</GroupBox>
...
</Grid>
</GroupBox>
因此,虽然GroupBox只能包含一个子节点,但是没有什么能阻止该子节点成为另一个可以包含所需元素的容器类型对象。
我在这里使用网格纯粹是为了说明目的。 您最终使用的容器将取决于您如何布局文本框和标签。 网格使您可以更好地控制元素的布局,但是从堆栈面板添加和删除元素更容易 - 您不必添加新的行或列定义。
但是,如果您的列表是可变长度,那么您最好为列表项创建DataTemplate:
<DataTemplate x:Key="ListTemplate">
<GroupBox>
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<TextBox .../>
<TextBox .../>
</StackPanel>
</GroupBox>
</DataTemplate>
然后使用绑定显示列表中的元素:
<GroupBox Header=Student Details">
<ListBox ItemsSource={Binding StudentList}">
<ListBox.ItemTemplate>
....
</ListBox.ItemTemplate/>
</ListBox>
</GroupBox>
我找到了以下解决方案链接
<ItemsControl ItemsSource="{Binding SomeCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=.}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我希望它会对你有所帮助。
您可以使用此xaml内容创建UserControl来表示您的学生数据:
StudentControl.xaml
<UserControl x:Class="YourNamespace.StudentControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignWidth="300">
<GroupBox Margin="3">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Content="Student 1" x:Name="StudentLabel" FontWeight="Bold"></Label>
<TextBox Grid.Column="1" Text="name" Margin="3"></TextBox>
<TextBox Grid.Column="2" Text="email" Margin="3"></TextBox>
</Grid>
</GroupBox>
</UserControl>
StudentControl.xaml.cs(代码背后)
using System.Windows.Controls;
namespace YourNamespace
{
public partial class StudentControl : UserControl
{
public StudentControl(string labelText)
{
InitializeComponent();
StudentLabel.Content = labelText;
}
}
}
然后在您的主窗口中,这是您需要的所有标记:
<Grid VerticalAlignment="Top">
<GroupBox Header="Student Details">
<UniformGrid x:Name="StudentGrid" Columns="2" />
</GroupBox>
</Grid>
并在代码隐藏文件(MainWindow.xaml.cs)中:
public MainWindow()
{
InitializeComponent();
for (var i = 0; i < 20; i++)
StudentGrid.Children.Add(new StudentControl($"Student{i + 1}"));
}
如果您想拥有网格布局的强大功能,但是您确定要为所有用户控件平均分配空间,那么使用UniformGrid非常棒。
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.