WPF实现多个RichTextBox联动 提示未处理 System.NullReferenceException(求大神们帮忙啊!!)
背景WPF实现多个RichTextBox联动
输入数字和英文时是没有问题的,但输入中文是就提示如图错误
我的代码如下:
MyRichTextBox Myitem = (((MyRichText.Parent as Grid).Parent as StackPanel).Parent as Grid).Parent as MyRichTextBox;
if (Myitem ==null)
{
return;
}
foreach (var item in sp.Children)
{
if ((item as MyRichTextBox) != Myitem)
{
(item as MyRichTextBox).richTextBox1.TextChanged -= new TextChangedEventHandler(richTextBox1_TextChanged);
(item as MyRichTextBox).richTextBox3.TextChanged -= new TextChangedEventHandler(richTextBox1_TextChanged);
}
}
for (int i = 0; i < sp.Children.Count; i++)
{
string Myxml = System.Windows.Markup.XamlWriter.Save(MyRichText.Document);
System.IO.StringReader MystrReader = new System.IO.StringReader(Myxml);
XmlReader MyXmlReader = System.Xml.XmlReader.Create(MystrReader);
object MyObj = System.Windows.Markup.XamlReader.Load(MyXmlReader);
// FlowDocument MyFlowDocument = MyObj as FlowDocument;
if (MyObj as FlowDocument == null)
{
return;
}
FlowDocument MyFlowDocument = MyObj as FlowDocument;
if ((sp.Children[i] as MyRichTextBox) != Myitem)
{
if (MyRichText.Name == "richTextBox1")
{
(sp.Children[i] as MyRichTextBox).richTextBox1.Document = MyFlowDocument;
}
if (MyRichText.Name == "richTextBox3")
{
(sp.Children[i] as MyRichTextBox).richTextBox3.Document = MyFlowDocument;
}
}
MystrReader.Dispose();
}
foreach (var item in sp.Children)
{
if ((item as MyRichTextBox) != Myitem)
{
(item as MyRichTextBox).richTextBox1.TextChanged += new TextChangedEventHandler(richTextBox1_TextChanged);
(item as MyRichTextBox).richTextBox3.TextChanged += new TextChangedEventHandler(richTextBox1_TextChanged);
}
}