【发布时间】:2026-01-28 22:50:01
【问题描述】:
所以目前我已经成功地从 button_click 上的代码隐藏生成单行重复元素。但是,我需要它们在每次单击按钮时生成。 IE。每次点击生成 + 1 行。 这是我的代码 sn-p 关于它是如何生成的:
// Items to populate Simulator Transaction Type List!r
private class Item
{
public string Name;
public Item(string name)
{
Name = name;
}
public override string ToString()
{
// Generates the text shown in the combo box
return Name;
}
}
public void btnAddSimFields_Click(object sender, RoutedEventArgs e)
{
var newTypeBox = new ComboBox();
{
newTypeBox.Height = 22;
newTypeBox.Width = 137;
newTypeBox.Margin = new Thickness(10, 90, 323, 0);
newTypeBox.VerticalAlignment = VerticalAlignment.Top;
newTypeBox.Text = "Select an Action...";
newTypeBox.IsEditable = true;
newTypeBox.Items.Add(new Item("Buy"));
newTypeBox.Items.Add(new Item("Sell"));
newTypeBox.Items.Add(new Item("Sell Full Amount"));
}
var newCoinBox = new ComboBox();
{
newCoinBox.Height = 22;
newCoinBox.Width = 137;
newCoinBox.Text = "Select a coin";
newCoinBox.VerticalAlignment = VerticalAlignment.Top;
newCoinBox.Margin = new Thickness(166, 90, 167, 0);
newCoinBox.ItemsSource = cmbListCoins.Items;
newCoinBox.IsEditable = true;
}
var newWaterMark = new WatermarkTextBox();
{
newWaterMark.Height = 22;
newWaterMark.Width = 120;
newWaterMark.HorizontalAlignment = HorizontalAlignment.Left;
newWaterMark.VerticalAlignment = VerticalAlignment.Top;
newWaterMark.Margin = new Thickness(323, 90, 27, 0);
newWaterMark.Watermark = "Quantity...";
}
gridSimBoxes.Children.Add(newTypeBox);
gridSimBoxes.Children.Add(newCoinBox);
gridSimBoxes.Children.Add(newWaterMark);
}
所以我在这里添加它们是正确的,它们会生成一切都很好而且花花公子。我的一般问题是如何在每次单击时继续添加它们,我正在讨论 foreach 或 for 循环。我很困惑。它们还应该能够绑定到一组全球可用的资源。
提前致谢,
维克多, 失去的实习生
【问题讨论】:
标签: c# wpf combobox code-behind dynamically-generated