- 在我的 C# WPF windows 应用程序中,我使用了带有一些标签和文本框的组框来插入一些数据
- 我在该组框中的一个文本框旁边创建了一个名为(添加)的按钮
- 当我按下(添加)按钮时,我想要:
- Group Box 扩大尺寸和
- 第一个文本框下方出现一个新文本框以插入更多数据
- (添加)按钮出现在新文本框旁边
- 我希望你能帮助我..谢谢
问问题
1283 次
1 回答
2
看看这是否是您想要的,我使用StackPanel's
了DockPanel
,并且随着内容的添加会增长,我还添加了 5 个额外的TextBox's
并且TextBlock's
具有折叠的可见性,这意味着它们在不可见时不会占用任何空间。我将它们添加到列表中,然后在您单击按钮时检查可见性,使相应的控件在那时可见。
主窗口.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="214" Width="525" SizeToContent="WidthAndHeight">
<Grid>
<GroupBox Header="General Information" HorizontalAlignment="Left" Name="groupBox1" VerticalAlignment="Top">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<DockPanel Grid.Column="0" HorizontalAlignment="Left" >
<Button VerticalAlignment="Bottom" Height="25" Click="Button_Click" >Add</Button>
</DockPanel>
<StackPanel Name="stackpanelTB" Grid.Column="1" Orientation="Vertical" >
<StackPanel.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="0,4,0,0"/>
</Style>
</StackPanel.Resources>
<TextBox Name="textBox1" Width="240" Height="25"></TextBox>
<TextBox Name="textBox2" Width="240" Height="25"></TextBox>
<TextBox Name="textBox3" Width="240" Height="25"></TextBox>
<TextBox Name="textBox4" Width="240" Height="25"></TextBox>
<TextBox Name="textBox5" Width="240" Visibility="Collapsed" Height="25"></TextBox>
<TextBox Name="textBox6" Width="240" Visibility="Collapsed" Height="25"></TextBox>
<TextBox Name="textBox7" Width="240" Visibility="Collapsed" Height="25"></TextBox>
<TextBox Name="textBox8" Width="240" Visibility="Collapsed" Height="25"></TextBox>
<TextBox Name="textBox9" Width="240" Visibility="Collapsed" Height="25"></TextBox>
</StackPanel>
<StackPanel Name ="stackpanelCaption" Grid.Column="2" Orientation="Vertical">
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="0,4,0,0"/>
</Style>
</StackPanel.Resources>
<TextBlock Name="textBlock1" HorizontalAlignment="Right" Height="25">Ingredient ID</TextBlock>
<TextBlock Name="textBlock2" HorizontalAlignment="Right" Height="25">Ingredient Name</TextBlock>
<TextBlock Name="textBlock3" HorizontalAlignment="Right" Height="25">Chemical Str</TextBlock>
<TextBlock Name="textBlock4" HorizontalAlignment="Right" Height="25">Ingredient Salts</TextBlock>
<TextBlock Name="textBlock5" HorizontalAlignment="Right" Visibility="Collapsed" Height="25"></TextBlock>
<TextBlock Name="textBlock6" HorizontalAlignment="Right" Visibility="Collapsed" Height="25"></TextBlock>
<TextBlock Name="textBlock7" HorizontalAlignment="Right" Visibility="Collapsed" Height="25"></TextBlock>
<TextBlock Name="textBlock8" HorizontalAlignment="Right" Visibility="Collapsed" Height="25"></TextBlock>
<TextBlock Name="textBlock9" HorizontalAlignment="Right" Visibility="Collapsed" Height="25"></TextBlock>
</StackPanel>
</Grid>
</GroupBox>
</Grid>
</Window>
主窗口.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
List<TextBox> tboxList = new List<TextBox>();
List<TextBlock> tblockList = new List<TextBlock>();
public MainWindow()
{
InitializeComponent();
tblockList.Add(textBlock1);
tblockList.Add(textBlock2);
tblockList.Add(textBlock3);
tblockList.Add(textBlock4);
tblockList.Add(textBlock5);
tblockList.Add(textBlock6);
tblockList.Add(textBlock7);
tblockList.Add(textBlock8);
tblockList.Add(textBlock9);
tboxList.Add(textBox1);
tboxList.Add(textBox2);
tboxList.Add(textBox3);
tboxList.Add(textBox4);
tboxList.Add(textBox5);
tboxList.Add(textBox6);
tboxList.Add(textBox7);
tboxList.Add(textBox8);
tboxList.Add(textBox9);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 9; i++)
{
if (tboxList[i].Visibility == Visibility.Collapsed)
{
tboxList[i].Visibility = Visibility.Visible;
tblockList[i].Visibility = Visibility.Visible;
break;
}
}
}
}
}
于 2013-01-01T04:16:18.330 回答