添加文本框和以编程方式按钮 ->没有文本框内容的按钮单击事件
我对此很陌生,到目前为止我还没有在网上找到任何解决方案来解决我的问题。
我想通过以编程方式添加控件来使用控件,这可以工作并且内容显示在窗口中,但是一旦我想通过按钮保存内容,事件处理程序就无法获取传递给它的变量。
我遇到以下情况,我不知道我错过了什么。 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题不是 WPF 问题,而是面向对象计算机编程的非常基本的问题。
您如何期望在
CheckMatching
方法中本地删除和创建的对象 (textBox2
) 在另一种方法(如SaveButton_Click)中可用?
为此,您可以将其范围限定为类级别。
然后你也可以用WPF的方式来做......
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 inCheckMatching
method be available in another method likeSaveButton_Click
?For that you can scope it class level.
And then you can also do it WPF way....