我是新來的WPF和相當新的C#和我沿着頭第一個C#第二版。第218頁(但是一個WPF版本,而不是WinForms版本)。我的表單上有一個TextBox
,我想在窗體加載時顯示一個默認字符串。我有兩個問題:如何在WPF應用程序中設置默認值?
如果我在XAML代碼中設置
Text
屬性,在窗體加載的TextChanged
事件觸發,我得到一個NullReferenceException
。這是我的XAML代碼段:<TextBox Name="cakeWritingText" Text="Happy Birthday" TextChanged="cakeWritingText_TextChanged" />
如果我把我的代碼,而不是值(和刪除XAML
Text
屬性)是這樣的:cakeWritingText.Text = "Happy Birthday";
,我得到這個錯誤:
No symbols are loaded for any call stack frame. The source code cannot be displayed.
以下是完整的事件處理程序:
private void cakeWritingText_TextChanged(object sender, TextChangedEventArgs e)
{
birthdayParty.CakeWriting = cakeWritingText.Text;
DisplayBirthdayPartyCost();
}
和MainWindow
構造:
public MainWindow()
{
InitializeComponent();
dinnerParty = new DinnerParty(5, (bool)healthyCheckBox.IsChecked, (bool)fancyCheckBox.IsChecked);
DisplayDinnerCost();
birthdayParty = new BirthdayParty(5, (bool)fancyBdayCheckBox.IsChecked, cakeWritingText.Text);
numPeopleBdayUpDown.Value = 5;
cakeWritingText.Text = "Happy Birthday";
DisplayBirthdayPartyCost();
}
什麼是放在一個TextBox
默認的字符串的最佳方式?