這裏是基本的WPF示例,請參閱代碼並嘗試完成您的示例。
View實現
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<Label x:Name="lblLabel"/>
<TextBox x:Name="txtFullName" GotFocus="TextBox_GotFocus" />
<TextBox x:Name="txtPhoneNumber" GotFocus="TextBox_GotFocus" />
<Button x:Name="btnOk" Content="OK" Click="btnOk_Click"/>
</StackPanel>
代碼隱藏
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
var textBox = (TextBox)sender;
string labelText = "";
if (textBox.Name == "txtFullName")
{
labelText = "Enter your full name.";
}
if (textBox.Name == "txtPhoneNumber")
{
labelText = "Enter your phone number and area code.";
}
lblLabel.Content = labelText;
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(string.Format("Full name: {0}, phone number: {1}", txtFullName.Text, txtPhoneNumber.Text));
}
}
沒有一個真正的問題在這裏,是嗎? – 2014-10-09 22:46:51
對我來說不幸的是,我的Visual Studio老師根本沒有幫助。他認爲他的時間比我的成績更有價值。 – 2014-10-09 22:49:58
我設置了表單,除了幾行外,其他所有內容都已完成。我不知道如何在單擊文本框時使標籤更改文本 – 2014-10-09 22:50:44