简体   繁体   English

动态创建的“ TextBox”没有价值

[英]Dynamically created `TextBox` is not getting value

I have created one TextBox , PasswordBox and Button for ClickEvent dynamically. 我已经为ClickEvent动态创建了一个TextBoxPasswordBoxButton When user press student or teacher Button . 当用户按下学生或老师Button Now Problem is that TextBox is not getting value when I click on it. 现在的问题是,当我单击它时, TextBox无法获得价值。 Mean as I click on textfield curser should bling on it and get value I wrote in it. 意思是当我单击textfield时,curser应该对其启用并获取我在其中编写的值。 but it is not getting it. 但是没有得到。 PasswordBox is working fine. PasswordBox工作正常。 Sorry for bad English. 对不起,英语不好。 here is my code 这是我的代码

        TextBlock name_block = new TextBlock();
        name_block.Text = "Enter your ID : ";
        name_block.Margin = new Thickness(5, 8, 0, 0);
        container.Children.Add(name_block);

        TextBox name_box = new TextBox();
        name_box.Width = 200;
        name_box.Height = 25;
        name_box.IsEnabled = true;
        name_box.Margin = new Thickness(150, 5, 1, 1);
        container.Children.Add(name_box);


        TextBlock pass_block = new TextBlock();
        pass_block.Text = "Enter your password : ";
        pass_block.Margin = new Thickness(5, 78, 0, 0);
        container.Children.Add(pass_block);

        PasswordBox pass_box = new PasswordBox();
        pass_box.Width = 200;
        pass_box.Height = 25;
        pass_box.IsEnabled = true;
        pass_box.Margin = new Thickness(150, 75, 0, 0);
        container.Children.Add(pass_box);

        Button login_btn = new Button();
        login_btn.Content = "Login";
        login_btn.Height = 25;
        login_btn.Margin = new Thickness(150, 150, 0, 0);
        container.Children.Add(login_btn);

        teacher_btn.IsEnabled = false;

        login_btn.Click += Teacher_Login_btn_Click;
    }

I think you are using Grid with out setting grid columns and rows. 我认为您在使用Grid时未设置网格列和行。 Seeing your code you can use canvas layout container as you are setting location for your controls. 看到代码后,可以在设置控件的位置时使用画布布局容器。

        <Canvas Name="container" HorizontalAlignment="Left" Height="301" Margin="10,10,0,0" VerticalAlignment="Top" Width="499" />

Rest of your code works if you are using canvas container. 如果您使用的是画布容器,则其余代码也可以使用。

otherwise let me know which layout container you are using. 否则,请让我知道您正在使用哪个布局容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM