C#空对象属性来自另一个类的列表

时间:2019-04-06 01:23:29

标签: c#

因此,我对列表中的对象有疑问。该列表位于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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

正如@Blorgbeard所说。 在ListaWybranychPrzedmiotow = StudentSubjects类中创建新的ListaWybranychPrzedmiotow = StudentSubjects.ToList()对象时,可以将Student更改为DeansOffice

谢谢!