2017-08-08 37 views
0

我正在使用C#(WPF)爲項目添加人員到列表中以顯示在第一個窗體(MainWindow)上;在MainWindow上單擊一個按鈕以打開一個新窗體(Window1),他們可以在其中添加有關要添加到列表中的人員的詳細信息(姓名,年齡等),但我無法傳遞這些數據。下面是我有:C#從另一個表格添加到列表

public partial class MainWindow : Window 
{ 
    public List<Patients> newPatientList = new List<Patients>(); 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 
    private void button_Click(object sender, RoutedEventArgs e) 
    { 
     Window1 addPx = new Window1(); 
     addPx.Show(); 
    } 

然後在窗口1中,單擊按鈕時:

MainWindow newPxList = new MainWindow(); 

newPxList.newPatientList.Add(new Patients(lstname.Text, frstname.Text, age.Text, 
              rm.Text, "", status.SelectedItem.ToString())); 

然後在窗口關閉,但似乎列表不原始表單上進行更新。

+2

您正在主窗口的新實例。你需要得到相同的實例。您可以將它作爲參數傳遞給構造函數中的Window1。 – ajg

+0

這是原因:*** new MainWindow(); ***請檢查@BWA回答/評論 –

回答

1
在窗口1

,你正在做的MainWindow新實例,就像你創建新認爲它,MainWindow的空白和隱藏副本。因此,您的原始MainWindow保持不變。

你需要做的是通過引用(假設鏈接)的MainWindowWindow1

要做到這一點,你需要做以下的事情:

作出財產Main,在你Window1類,並更改默認構造函數以獲取MainWindow參考,如下所示:

public MainWindow Main { get; set; } 

public Window1(MainWindow main) 
{ 
    InitializeComponent(); 
    this.Main = main; 
} 

現在,你必須改變構造函數時,你正在展示Window1,像這樣:

Window1 addPx = new Window1(this); 
addPx.Show(); 

張揚方法MainWindow,是這樣的:

public void AddNewPatient(string lastName, string FirstName, string age) 
{ 
    //add new ListItem here 
} 

而且,在Window1通話需要,在該方法時點擊按鈕,像這樣:

Main.AddNewPatient(lstname.Text, frstname.Text, age.Text /*etc */); 
+0

謝謝,工作很好。感謝您的解釋 – AlphaOmega

2

OnClick方法在Window1您創建新的實例MainWindow並添加新的病人到這個實例。在這種方法之後MainWindow的這個實例被銷燬。您應該將參考列表作爲參數Window1

例如,

private void button_Click(object sender, RoutedEventArgs e) 
{ 
    Window1 addPx = new Window1(newPatientList); 
    addPx.Show(); 
} 

而且在Window1 insted的

newPxList.newPatientList.Add(new Patients(lstname.Text, frstname.Text, age.Text, 
              rm.Text, "", status.SelectedItem.ToString())); 

事情是這樣的:

patientListFromMainWindow.Add(new Patients(lstname.Text, frstname.Text, age.Text, 
               rm.Text, "", status.SelectedItem.ToString())); 
2

您需要將列表的引用傳遞到新窗口

public partial class MainWindow : Window 
{ 
    public List<Patients> newPatientList = new List<Patients>(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 
    private void button_Click(object sender, RoutedEventArgs e) 
    { 
     Window1 addPx = new Window1(newPatientList); 
     addPx.Show(); 
    } 
} 

public partial class Window1 : Window 
{ 
    List<Patients> _patients; 
    public Window1(List<Patients> patients) 
    { 
     InitializeComponent(); 
     _patients = patients; 
    } 
    private void button_Click(object sender, RoutedEventArgs e) 
    { 
    _patients.Add(new Patients(lstname.Text, frstname.Text, age.Text, 
             rm.Text, "", status.SelectedItem.ToString())); 
    } 
} 
1

不是傳遞任何額外的參數到Window1構造函數,你可以利用ShowDialog()方法,並在關閉對話框剛剛獲得新創建的患者。現在

private void button_Click(object sender, RoutedEventArgs e) 
{ 
    Window1 wnd1 = new Window1(); 
    bool? dialogResult = wnd1.ShowDialog(); 
    if (dialogResult.HasValue && dialogResult.Value) 
    { 
     newPatientList.Add(wnd1.Patient); 
    } 
} 

可以調整Window1撐起結果:

class Window1 : Window 
{ 
    Patient _patient; 
    public Patient Patient 
    { 
     get { return _patient; } 
    } 

    // rest of your code 

    void button_Click(object sender, RoutedEventArgs e) 
    { 
     _patient = new Patients(lstname.Text, frstname.Text, age.Text, rm.Text, "", status.SelectedItem.ToString()); 
     DialogResult = true; 
     Close(); 
    } 
} 
相關問題