首先,这仅用于测试目的
我有一个仅包含一个按钮和 2 个堆栈面板的 xaml 页面。想法是有一个类,该类将具有与之关联的代码和名称。将查询代码并根据结果显示适当数量的文本框。我需要能够在它的末尾添加名称到文本框并保存列表。
这是我当前的代码,它以我想要的方式创建文本框,但在填写后不太确定是否保存列表
xml
<StackPanel x:Name="StackSG" Grid.Row="1" Grid.Column="0">
<ListBox ItemsSource="{Binding StackSG}" />
</StackPanel>
<StackPanel x:Name="StackSGName" Grid.Row="1" Grid.Column="1">
<ListBox ItemsSource="{Binding StackSGName}" />
</StackPanel>
<Button x:Name="Generate" Content="Generate" Height="23" Width="75" Grid.Row="0" Grid.Column="0"
Command="{Binding Path=GenerateSGKeys}"/>
</Grid>
</UserControl>
它连接到我的视图模型,看起来像这样
public class stackpnl : Notify
{
private IEnumerable stackSG;
public IEnumerable StackSG
{
get { return stackSG; }
set { stackSG = value; OnNotifyPropertyChanged("StackSG"); }
}
private IEnumerable stackSGName;
public IEnumerable StackSGName
{
get { return stackSGName; }
set { stackSGName = value; OnNotifyPropertyChanged("StackSGName"); }
}
private DelegateCommand generateSGKeys;
public ICommand GenerateSGKeys
{
get { return generateSGKeys; }
}
private IEnumerable allotments;
public IEnumerable Allotments
{
get { return allotments; }
set { allotments = value; OnNotifyPropertyChanged("Allotments"); }
}
TextBox txtSGName = new TextBox();
public stackpnl()
{
generateSGKeys = new DelegateCommand(Generate, OnGenerate);
StackSG = new List<TextBox>();
StackSGName = new List<TextBox>();
}
private bool OnGenerate(object obj)
{
return true;
}
public class Allotment
{
#region Properties
public string AllotmentName { get; set; }
public string AllotmentCode { get; set; }
#endregion
}
private void Generate(object obj)
{
IList<TextBox> StackSGTmp = new List<TextBox>();
IList<TextBox> StackSGNameTmp = new List<TextBox>();
IList<Allotment> newList = new List<Allotment>();
int st = 10;
for (int i = 0; i < st; i++)
{
newList.Add(new Allotment() { AllotmentCode = string.Format("{0}{1}", "Code", i.ToString()), AllotmentName = string.Format("{0}{1}", "Code", i.ToString()) });
}
foreach (var Allotment in newList)
{
TextBox txtSG = new TextBox();
txtSG.Name = string.Format(Allotment.AllotmentCode);
txtSG.Height = 25;
txtSG.Width = 75;
txtSG.Text = string.Format(Allotment.AllotmentCode);
txtSG.Visibility = System.Windows.Visibility.Visible;
StackSGTmp.Add(txtSG);
//Add SG name textboxes
txtSGName = new TextBox();
txtSGName.Name = string.Format(string.Format("{0}{1}", Allotment.AllotmentCode, Allotment.AllotmentName));
txtSGName.Height = 25;
txtSGName.Width = 75;
txtSGName.SetBinding(TextBox.TextProperty, new Binding(Allotment.AllotmentName) { Mode = BindingMode.TwoWay });
txtSGName.Visibility = System.Windows.Visibility.Visible;
txtSGName.KeyDown += new KeyEventHandler(txtSGName_KeyDown);
StackSGNameTmp.Add(txtSGName);
}
StackSG = StackSGTmp;
StackSGName = StackSGNameTmp;
}
void txtSGName_KeyDown(object sender, KeyEventArgs e)
{
BindingExpression binding = (sender as TextBox).GetBindingExpression(TextBox.TextProperty);
binding.UpdateSource();
}
}