我有一个名为UserIdentity的类
class UserIdentity
{
public string Name {get; set;}
public string Gender {get; set;}
public string Age {get; set;}
public string Department {get;set;}
}
我正在尝试使用按钮来保存用户数据
private void UserID_Save_Click(object sender, RoutedEventArgs e)
{
UserIdentity user = new UserIdentity()
{
Name = textUserName.Text,
Age = textUserAge.Text,
Gender = textUserGender.Text,
Department = textUserDepartment.Text
};
}
当我直接从textboxt表单显示用户数据
时,它工作得很好 private void ShowUser_Click(object sender, RoutedEventArgs e)
{
DisplayName.Text = textUserName.Text,
DisplayAge.Text = textUserAge.Text,
DisplayGender.Text = textUserGender.Text,
DisplayDepartment.Text = textUserDepartment.Text,
}
但是我试图从一个对象显示用户数据,但它给了我“当前上下文中不存在'用户'这个名字”
private void ShowUser_Click(object sender, RoutedEventArgs e)
{
DisplayName.Text = user.Name;
DisplayAge.Text = user.Age;
DisplayGender.Text = user.Gender;
DisplayDepartment.Text = user.Department;
}
这不起作用
private void ShowUser_Click(object sender, RoutedEventArgs e)
{
DisplayName.Text = UserIdentity.Name;
DisplayAge.Text = UserIdentity.Age;
DisplayGender.Text = UserIdentity.Gender;
DisplayDepartment.Text = UserIdentity.Department;
}
我该如何解决这个问题?
答案 0 :(得分:0)
将用户带到全球:
UserIdentity user;
private void UserID_Save_Click(object sender, RoutedEventArgs e)
{
user = new UserIdentity()
{
Name = textUserName.Text,
Age = textUserAge.Text,
Gender = textUserGender.Text,
Department = textUserDepartment.Text
};
}