我有一個WPF專案,我想將DataRows從DataGrid中保存到一個 "選項 "類中,并在另一個視窗中檢索這些變數。
這就是我如何將我的變數從我的DataGrid保存到我的 "選項 "類(mainWindow.xaml.cs):
options.title = Convert.ToString((showEntries.SelectedItem as DataRowView).Row["title"/span>]);
這個變數通過一個getter和setter(options.cs)來保存:
public string Title
{
get { return title; }
set { title = value; }
}
現在我想在另一個視窗(updateDatabse.xaml)中檢索保存的變數:
private void getUpdateEntries()
{
Options returnValues = new Options()。
titleBox.Text = returnValues.Title。
我的問題是:為什么我的文本框 "titleBox "在運行我的代碼時是空的。
CodePudding
如果你的任務邏輯沒有規定創建幾個類的實體(就我對你的解釋的理解,是這樣的),那么你可以使用Singlton實作。 例子:
public class Options
{
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private Options() { }
public static Options Instance { get; } = new Options()。
}
Options.Instance.Title = Convert.ToString((showEntries.SelectedItem as DataRowView).Row["title"] ) 。
private void getUpdateEntries()
{
titleBox.Text = Options.Instance.Title。
}
uj5u.com熱心網友回復:
你把事情搞混了。
private void getUpdateEntries()
{
Options returnValues = new Options()。
returnValues.title = Convert.ToString((showEntries. SelectedItem as DataRowView).Row["title"/span>] )。)
titleBox.Text = returnValues.Title。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/307885.html
標籤:
