将 XAML TextBlock 引用到 .cs 代码?

Reference XAML TextBlock to .cs code?

首先感谢您抽出时间,现在 我的问题是这个; 我的主 XAML 文件中有不同的文本块,我试图从另一个文件(.cs 文件)中获取它们的文本以在 sqlquery 中使用它们

    public void FillTable()
    {
        MainWindow l = new MainWindow();

        string sql = "insert into Pacientes (nombre) values ('"+ l.nombre_text.Text +"')";
        var command = new SQLiteCommand(sql, m_dbConnection);
        command.ExecuteNonQuery();
        l.Close();                

    } 

然而,当我检查 Table 结果为空,当我检查 table "nombre" 列为空白

See sql image

知道我做错了什么吗?

你做不到 "new MainWindow"。您需要获取对 MainWindow 的引用或以某种方式将文本传递给 FillTable()。

在没有看到更多代码的情况下,不可能找到确切的解决方案,但按照这些思路可能会让您畅通无阻。

...
// in MainWindow.cs
FillTable(this);
...

public void FillTable(MainWindow window)
{
    string sql = "insert into Pacientes (nombre) values ('"+ window.nombre_text.Text +"')";
    var command = new SQLiteCommand(sql, m_dbConnection);
    command.ExecuteNonQuery();   
} 

...
FillTable(nombre_text.Text);
...

public void FillTable(string nombre)
{
    string sql = "insert into Pacientes (nombre) values ('"+ nombre +"')";
    var command = new SQLiteCommand(sql, m_dbConnection);
    command.ExecuteNonQuery();   
}