因此,我对列表中的对象有疑问。该列表位于Student
类中,名为ListaWybranychPrzedmiotow
,我将其初始化并填充DeansOffice
类中的对象。
当我尝试在DeansOffice
类的MessageBoxes中显示它们时,通常会显示它,但是当我尝试从MainWindow
类显示相同的MessageBoxes时,相同的List为空,并且没有对象显示在MessageBoxes中。
我在做什么错了?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
GetDataFromDb();
StudentsDataGrid.ItemsSource = Student._ListaStudentow;
}
private void AddButton_Click(object sender, RoutedEventArgs e)
{
var wnd = new DeansOffice();
wnd.ShowDialog();
if (wnd.NewStudent != null)
{
var NewStudent = wnd.NewStudent;
foreach (Subject s in NewStudent.ListaWybranychPrzedmiotow)
MessageBox.Show(s.Name); //HERE they don't appear
}
}
}
public partial class DeansOffice : Window
{
public Student NewStudent { get; set; }
public List StudentSubjects = new List();
public DeansOffice()
{
InitializeComponent();
StudiaComboBox.ItemsSource = Studies._StudiesList;
PrzedmiotyListBox.ItemsSource = Subject._SubjectList;
}
private void AddButton_Click(object sender, RoutedEventArgs e)
{
var tmpNazwisko = NazwiskoTextBox.Text;
var tmpImie = ImieTextBox.Text;
var tmpNrIndeksu = NrIndeksuTextBox.Text;
var StudentSubjects = Subject._SubjectList.Where(p => p.IsChecked == true);
var AreSubjectsSelected = StudentSubjects.Any();
if (tmpImie != "" && tmpNazwisko != "" && tmpNrIndeksu != "" && StudiaComboBox.SelectedItem != null && AreSubjectsSelected)
{
var match = Regex.Match(tmpNrIndeksu, "^s[0-9]{4,5}$");
var tmpStudia = ((Studies)StudiaComboBox.SelectedItem).Id;
if (match.Success)
{
NewStudent = new Student
{
Imie = tmpImie,
Nazwisko = tmpNazwisko,
NrIndeksu = tmpNrIndeksu,
IdStudia = tmpStudia,
ListaWybranychPrzedmiotow = StudentSubjects
};
foreach (Subject s in NewStudent.ListaWybranychPrzedmiotow)
MessageBox.Show(s.Name); //HERE CheckBoxes appear
Close();
}
}
}
}
答案 0 :(得分:0)
正如@Blorgbeard所说。
在ListaWybranychPrzedmiotow = StudentSubjects
类中创建新的ListaWybranychPrzedmiotow = StudentSubjects.ToList()
对象时,可以将Student
更改为DeansOffice
。
谢谢!