【问题标题】:What is the best way to add large number of text box in wpf window在 wpf 窗口中添加大量文本框的最佳方法是什么
【发布时间】:2016-12-10 17:01:42
【问题描述】:

我正在尝试制作一个全屏(不包括任务栏)并包含大量文本框的 WPF 窗口。

我以前在 winforms 中做过这个。但是在 winforms 中,很容易制作并将所有元素放在一个 groupbox 中以使其看起来更好,但是如何在 WPF 中执行此操作,因为在 WPF groupbox 中可以包含单个项目。

我想做这样的事情:

【问题讨论】:

  • 我找到了以下link希望对你有帮助。

标签: c# wpf


【解决方案1】:

如果列表是固定长度,则将文本框放在另一个容器中,例如 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>

【讨论】:

    【解决方案2】:
    <GroupBox Header="Student Details">
       <StackPanel>
           <GroupBox>....</GroupBox>
           <GroupBox>....</GroupBox>
           <GroupBox>....</GroupBox>
       </StackPanel>
    </GroupBox>
    

    【讨论】:

    • 谢谢。盒子的大小怎么样。我想把它装在全窗里。我应该拖动它们还是我的意思是.. 如何调整框的大小
    • GroupBox 默认拉伸。在 GroupBox 中,您可以使用带有列的 Grid,例如
    【解决方案3】:

    我找到了以下解决方案link

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

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

      您可以创建一个 UserControl 来使用此 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(代码隐藏)

      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 非常好。

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-15
        • 1970-01-01
        • 2020-02-06
        • 1970-01-01
        • 2021-09-15
        • 2011-07-28
        • 2019-11-12
        相关资源
        最近更新 更多