我有一个WPF主窗口,其中包含一个带有按钮的工具栏和一个显示带有列表框的页面的tabcontrol。该页面托管在一个框架上,并且框架设置在我选择的选项卡上。将项目添加到列表框
当我点击我的工具栏上的一个按钮时,会弹出一个新窗口,显示一个文本框和一个提交按钮。当我按提交按钮时,我想将文本框内容插入到主窗口上的列表框中。但是,列表框中没有显示任何内容。我试过listbox.Items.Add(),但它仍然不会显示。
public partial class AddNewCamper : Window
{
public AddNewCamper()
{
InitializeComponent();
}
private void btnNewSubmit_Click(object sender, RoutedEventArgs e)
{
CampersPage c;
// Converting string to int b/c thats what camper() takes in.
int NewAge = Convert.ToInt16(txtNewAge.Text);
int NewGrade = Convert.ToInt16(txtNewGrade.Text);
// Create a new person
Camper person = new Camper(NewAge, NewGrade, txtNewFirstName.Text);
txtNewFirstName.Text = person.getName();
// Trying to add the first name of the person to display on the listbox of another window.
c.testListBox.Items.Add(txtNewFirstName.Text);
}
'CampersPage c;'在此之后,'c'不会在任何地方初始化。可能有一个NullReferenceException被吞噬了。 –
而我不认为我正确地将字符串文本转换为int – TMan
是的,我知道你的意思。但是,如果我创建camperPages c = new camperPage,那么它将成为一个全新的页面,我已经在主窗口代码中创建了它。 – TMan