【发布时间】:2016-01-19 18:19:31
【问题描述】:
基本上,我正在制作一个程序,允许您通过按下按钮向堆栈面板添加另一个具有多个水平对齐文本框的堆栈面板。到目前为止,一切都按预期进行。到目前为止,这是我的代码,Stacker 是父堆栈面板的名称,它一开始是空的:
private void Add_Click(object sender, RoutedEventArgs e)
{
Stacker.Children.Add(NewXD(Stacker.Children.Count + 1));
}
public System.Windows.Controls.StackPanel NewXD(int XD)
{
System.Windows.Controls.StackPanel NewP = new StackPanel();
NewP.Orientation = Orientation.Horizontal;
System.Windows.Controls.TextBox HAHA = new TextBox();
HAHA.Name = "question" + XD.ToString();
//HAHA.Text = HAHA.Height.ToString()+" "+HAHA.Width.ToString();
HAHA.Height = Double.NaN;
HAHA.Width = 120;
HAHA.FontSize=20;
NewP.Children.Add(HAHA);
for (int i = 1; i < 6; i++)
{
System.Windows.Controls.TextBox newBox = new TextBox();
newBox.Name = "answer"+XD.ToString()+"_"+i.ToString();
newBox.Height = Double.NaN;
newBox.Width = 120;
NewP.Children.Add(newBox);
}
System.Windows.Controls.ComboBox correct = new ComboBox();
correct.Name = "correct" + XD.ToString();
for (int i = 1; i < 6; i++)
{
System.Windows.Controls.ComboBoxItem newItem = new ComboBoxItem();
newItem.Name = "ans" + XD.ToString() + "_" + i.ToString();
newItem.Content = i.ToString();
correct.Items.Add(newItem);
}
NewP.Children.Add(correct);
return NewP;
}
对于我的一些代码不够认真,我深表歉意。
现在,我需要做的是让子堆栈面板也包含独立的文件选择器,这些选择器的工作方式类似于此线程中的示例:Open file dialog and select a file using WPF controls and C#
我不知道如何执行的是,这些生成的按钮中的每一个都具有相同的基本功能,但都与它们对应的每个文本框链接。
提前致谢:)
编辑:当我写这篇文章时,我突然想到也许我可以使用子堆栈面板的类似数组的属性来选择相应的文本框,因为文件选择器的文本框和按钮将始终是最后两个项目堆栈面板,但我不太确定如何执行此操作。
【问题讨论】:
标签: c# wpf visual-studio-2013