无法访问更新的标签内容

时间:2011-06-09 07:00:39

标签: c# wpf

编辑:已解决

我有一个标签,用数据库中的值填充。如果用户在下面的文本框中输入此值,我想更改背景。标签在屏幕上显示精细值,但是当我尝试匹配文本框的textchanged事件中的值时,它显示为null。

    public void button1_Click(object sender, RoutedEventArgs e)
    {          
        txtAnswer.Clear();
        txtAnswer.Background = Brushes.White;
        int number = r.Next(3) + 1;

        string queryEnglish = "SELECT englishVerb FROM verbTable WHERE (verbID = " + number + ")";
        string queryFrench = "SELECT frenchVerb FROM verbTable WHERE (verbID = " + number + ")";


        using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\verbs.mdf;Integrated Security=True;User Instance=True"))
        {
            con.Open();

            using (SqlCommand command = new SqlCommand(queryEnglish, con))
            {
                this.lblEnglishVerb.Content = (string)command.ExecuteScalar();
            }

            using (SqlCommand command = new SqlCommand(queryFrench, con))
            {
                this.lblFrenchVerb.Content = (string)command.ExecuteScalar();
            }
        }            
    }


    public void txtAnswer_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (txtAnswer.Text == lblFrenchVerb.Content.ToString())
            txtAnswer.Background = Brushes.LightGreen;

        if (txtAnswer.Text == "test")          
            txtAnswer.Background = Brushes.AliceBlue;
    }

1 个答案:

答案 0 :(得分:1)

Textchanged可能会在内容中没有任何内容的情况下被触发。所以在第一个txtAnswer_TextChanged上你可能什么也得不到。