简体   繁体   English

在wpf窗口中添加大量文本框的最佳方法是什么

[英]What is the best way to add large number of text box in wpf window

I am trying to make a WPF window which will a full screen (excluding task bar) and will contain a lots of text box. 我正在尝试制作一个全屏(不包括任务栏)的WPF窗口,并且将包含大量文本框。

I made this before in winforms. 我之前在winforms中做过这个。 But in winforms , its easy to make and put all the elements inside a groupbox to make it look better but how to do this in WPF, because in WPF groupbox can contain single item. 但是在winforms中,它很容易制作并将所有元素放在一个组框中以使其看起来更好但是如何在WPF中执行此操作,因为在WPF中,groupbox可以包含单个项目。

I want to make something like this: 我想做这样的事情:

在此输入图像描述

<GroupBox Header="Student Details">
   <StackPanel>
       <GroupBox>....</GroupBox>
       <GroupBox>....</GroupBox>
       <GroupBox>....</GroupBox>
   </StackPanel>
</GroupBox>

If you list is a fixed length you put the text boxes inside another container such as a Grid, StackPanel or WrapPanel: 如果列表是固定长度,则将文本框放在另一个容器(如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>

So, while the GroupBox can only contain one child, there's nothing stopping that child being another container type object that can contain all the elements you need. 因此,虽然GroupBox只能包含一个子节点,但是没有什么能阻止该子节点成为另一个可以包含所需元素的容器类型对象。

I used a Grid here purely for illustrative purposes. 我在这里使用网格纯粹是为了说明目的。 The container you end up using will depend on how you need to layout your text boxes and labels. 您最终使用的容器将取决于您如何布局文本框和标签。 A grid gives you more control over the layout of the elements, but it's easier to add and remove elements from a stack panel - you don't have to add new row or column definitions. 网格使您可以更好地控制元素的布局,但是从堆栈面板添加和删除元素更容易 - 您不必添加新的行或列定义。

However if your list is a variable length then you'd be better off creating a DataTemplate for the list item: 但是,如果您的列表是可变长度,那么您最好为列表项创建DataTemplate:

<DataTemplate x:Key="ListTemplate">
    <GroupBox>
        <StackPanel Orientation="Horizontal">
            <TextBlock .../>
            <TextBox .../>
            <TextBox .../>
        </StackPanel>
    </GroupBox>
</DataTemplate>

Then using bindings to display the elements in a list: 然后使用绑定显示列表中的元素:

<GroupBox Header=Student Details">
    <ListBox ItemsSource={Binding StudentList}">
        <ListBox.ItemTemplate>
             ....
        </ListBox.ItemTemplate/>
    </ListBox>
</GroupBox>

I found the following solution link 我找到了以下解决方案链接

<ItemsControl ItemsSource="{Binding SomeCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=.}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I hope it will help you. 我希望它会对你有所帮助。

You could create a UserControl to represent your Student Data with this xaml content: 您可以使用此xaml内容创建UserControl来表示您的学生数据:

StudentControl.xaml 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 (Code behind) StudentControl.xaml.cs(代码背后)

using System.Windows.Controls;

namespace YourNamespace
{
    public partial class StudentControl : UserControl
    {
        public StudentControl(string labelText)
        {
            InitializeComponent();

            StudentLabel.Content = labelText;
        }
    }
}

Then in your main window this is all markup you'll need: 然后在您的主窗口中,这是您需要的所有标记:

<Grid VerticalAlignment="Top">
    <GroupBox Header="Student Details">
        <UniformGrid x:Name="StudentGrid" Columns="2" />
    </GroupBox>
</Grid>

And in the code behind file (MainWindow.xaml.cs): 并在代码隐藏文件(MainWindow.xaml.cs)中:

public MainWindow()
{
    InitializeComponent();

    for (var i = 0; i < 20; i++)
        StudentGrid.Children.Add(new StudentControl($"Student{i + 1}"));
}

Using an UniformGrid is great if you want to have the power of a grid layout but you're sure you want to distribute your space equally for all the user controls. 如果您想拥有网格布局的强大功能,但是您确定要为所有用户控件平均分配空间,那么使用UniformGrid非常棒。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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