2017-10-13 37 views
0

因此,我刚开始使用WPF。我正试图创建一个程序来加速制作卡片的过程。我目前正努力尝试将两个文本块的文本追加到一个richtextblock中(为了创建具有其效果和风格文本的卡的描述)。 WPF说第二个文本块是“未定义的”。 这是我的代码。WPF - 试图追加文本块文本抛出“对象引用未设置为对象的实例”错误

private void EffectInput_TextChanged(object sender, TextChangedEventArgs e) 
     { 
      Paragraph effectText = new Paragraph(); 
      Paragraph flavorText = new Paragraph(); 

      effectText.Inlines.Add(EffectInput.Text); 
      flavorText.Inlines.Add(FlavorInput.Text); //This is the line throwing the error 

      Description.Document.Blocks.Clear(); 

      Description.Document.Blocks.Add(effectText); 
      Description.Document.Blocks.Add(flavorText); 
     } 

我是新来的,该怎么办?

回答

0

您在EffectInput_TextChanged函数。您必须以另一种方式访问​​FlavorInput中的文本。你可以将它存储在另一个变量中,只要文本改变就更新该变量。我不记得如何清除Paragraph对象,因此您必须对该部分进行试验。

Paragraph flavorText = new Paragraph(); 
Paragraph effectText = new Paragraph(); 

private void FlavorInput_TextChanged(object sender, TextChangedEventArgs e){ 
    flavorText.Inlines.Clear(); 
    flavorText.Inlines.Add(FlavorInput.Text); 
    updateBlocks(); 
} 

private void EffectInput_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    effectText.Inlines.Clear(); 
    effectText.Inlines.Add(EffectInput.Text); 
    updateBlocks(); 
} 

private void updateBlocks(){ 
    Description.Document.Blocks.Clear(); 
    Description.Document.Blocks.Add(effectText);   
    Description.Document.Blocks.Add(flavorText);  
} 
+0

工作!谢谢! –

相关问题