我有这段代码来显示/隐藏带有一些文本框的画布,当我按下按钮时,它会将数据从 TextBox1 提交到数据库。问题是我不知道如何在后面的 C# 代码中访问 TextBox1。
例如,这是我的一些 XAML 代码:
<ContentControl Background="{x:Null}" >
<ContentControl.Template>
<ControlTemplate>
<StackPanel Grid.Row="3" Height="500" Name="stack1" Width="280">
<Canvas x:Name="canvas1" Height="400" >
<TextBox Height="23" Name="TextBox1" Width="70" />
<Button Content="Submit" Name="submit_button" Click="submit_button_Click" />
</Canvas>
<ToggleButton x:Name="toggleshowhide" Content="Show/Hide" IsChecked="True" Height="50" />
</StackPanel>
<ControlTemplate.Triggers>
<Trigger SourceName="toggleshowhide" Property="IsChecked" Value="True">
<Setter TargetName="canvas1" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger SourceName="toggleshowhide" Property="IsChecked" Value="False">
<Setter TargetName="canvas1" Property="Visibility" Value="Hidden" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ContentControl.Template>
<Button Content="Button" Height="23" Name="submit" Width="74" />
</ContentControl>
这就是我想要实现的目标:
private void submit_button_Click(object sender, RoutedEventArgs e)
{
OleDbCommand cmd = new OleDbCommand("INSERT INTO Table VALUES (this.TextBox1.Text), con);
cmd.Connection = con;
int temp = cmd.ExecuteNonQuery();
if (temp > 0)
{
MessageBox.Show("OK", "Info !");
}
else
{
MessageBox.Show("Some text !", "Error");
}
如果有人可以提供帮助,我将不胜感激。:)