添加文本框和以编程方式按钮 ->没有文本框内容的按钮单击事件

发布于 2024-12-04 17:29:48 字数 1518 浏览 0 评论 0原文

我对此很陌生,到目前为止我还没有在网上找到任何解决方案来解决我的问题。

我想通过以编程方式添加控件来使用控件,这可以工作并且内容显示在窗口中,但是一旦我想通过按钮保存内容,事件处理程序就无法获取传递给它的变量。

我遇到以下情况,我不知道我错过了什么。 (WPF4、EF、VS2010)

在 XAML 中,我有一个网格,我想在其中添加例如。代码隐藏中的文本框和按钮,如

<Grid  Name="Grid1">
    <Grid.RowDefinitions>
        <RowDefinition Height="100"></RowDefinition>
        <RowDefinition Height="50"></RowDefinition>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="75*"></ColumnDefinition>
        <ColumnDefinition Width="25*"></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <TextBox Grid.Row="2"  Name="textBox1" />
</Grid >

在代码隐藏中:

private void CheckMatching ()
{
    TextBox textBox2 = new TextBox();
    textBox2.Text = "textbox to fill";
    textBox2.Name = "textBox2";

    Grid1.Children.Add(textBox2);

    Button SaveButton = new Button();
    SaveButton.Name = "Save";
    SaveButton.Content = "Save";

    SaveButton.Click += new RoutedEventHandler(SaveButton_Click);
    Grid1.Children.Add(SaveButton);
}

private void SaveButton_Click ( object sender, RoutedEventArgs e )
{
    // works fine 
    string ShowContent1 = textBox1.Text;

    // It doesn't show up in intellisense, so I cant use it yet
    string ShowContent2 = textBox2.Text;
}

我可以访问 XAML 中文本框的内容或 XAML 中设置的其他所有内容,但我无法获取在代码隐藏中设置的任何其他内容的内容。内容本身显示在窗口中。

我已经尝试过不同的方法。到目前为止没有任何效果。

I'm pretty new to this and so far I haven't found any solution online which solves my problem.

I'd like to use controls by adding them programmatically, which works and the contents shows in the window, but as soon I want to save the content via button, the event handler doesn't get the variables passed to it.

I've got the following situation where I don't know what I miss. (WPF4, EF, VS2010)

In XAML I have a grid where I'd like to add eg. a textbox and a button from code behind like

<Grid  Name="Grid1">
    <Grid.RowDefinitions>
        <RowDefinition Height="100"></RowDefinition>
        <RowDefinition Height="50"></RowDefinition>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="75*"></ColumnDefinition>
        <ColumnDefinition Width="25*"></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <TextBox Grid.Row="2"  Name="textBox1" />
</Grid >

In code-behind:

private void CheckMatching ()
{
    TextBox textBox2 = new TextBox();
    textBox2.Text = "textbox to fill";
    textBox2.Name = "textBox2";

    Grid1.Children.Add(textBox2);

    Button SaveButton = new Button();
    SaveButton.Name = "Save";
    SaveButton.Content = "Save";

    SaveButton.Click += new RoutedEventHandler(SaveButton_Click);
    Grid1.Children.Add(SaveButton);
}

private void SaveButton_Click ( object sender, RoutedEventArgs e )
{
    // works fine 
    string ShowContent1 = textBox1.Text;

    // It doesn't show up in intellisense, so I cant use it yet
    string ShowContent2 = textBox2.Text;
}

I can access the content of the textbox in XAML or everything else set in XAML, but I don't get the content of anything else I set in code-behind. The content itself is shown in the window.

I tried different approaches already. Nothing worked so far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2024-12-11 17:29:48

这个问题不是 WPF 问题,而是面向对象计算机编程的非常基本的问题。

您如何期望CheckMatching 方法中本地删除和创建的对象 (textBox2) 在另一种方法(如 SaveButton_Click)中可用?

为此,您可以将其范围限定为类级别。

 private TextBox textBox2;

 private void CheckMatching ()
 {
     this.textBox2 = new TextBox();
     this.textBox2.Text = "textbox to fill";
     this.textBox2.Name = "textBox2";

     Grid1.Children.Add(this.textBox2);
     .....
 }

 private void SaveButton_Click ( object sender, RoutedEventArgs e )
 {
       string ShowContent1 = textBox1.Text; // works fine
       string ShowContent2 = this.textBox2.Text; // should work too
 } 

然后你也可以用WPF的方式来做......

 private void SaveButton_Click ( object sender, RoutedEventArgs e )
 {
       string ShowContent1 = textBox1.Text; // works fine
       string ShowContent2 = ((TextBox)Grid1.Children[1]).Text; // should work too
 } 

This question is not a WPF issue but something very basic to the Object Oriented Computer Programming.

How can you expect that an object (textBox2) that was delcared and created locally in CheckMatching method be available in another method like SaveButton_Click?

For that you can scope it class level.

 private TextBox textBox2;

 private void CheckMatching ()
 {
     this.textBox2 = new TextBox();
     this.textBox2.Text = "textbox to fill";
     this.textBox2.Name = "textBox2";

     Grid1.Children.Add(this.textBox2);
     .....
 }

 private void SaveButton_Click ( object sender, RoutedEventArgs e )
 {
       string ShowContent1 = textBox1.Text; // works fine
       string ShowContent2 = this.textBox2.Text; // should work too
 } 

And then you can also do it WPF way....

 private void SaveButton_Click ( object sender, RoutedEventArgs e )
 {
       string ShowContent1 = textBox1.Text; // works fine
       string ShowContent2 = ((TextBox)Grid1.Children[1]).Text; // should work too
 } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文