0

我有一个名为 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

        };
    }

当我直接从文本框表单显示用户数据时,它工作得很好,

    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;

    }

我该如何解决?

4

1 回答 1

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

        };
    }
于 2012-10-17T20:31:24.587 回答