在下面的代码中,我随机遇到了InvalidOperationException,有什么好方法可以修复它?
public class ParsedTextBlock : TextBlock
{
static ParsedTextBlock() {
TextProperty.OverrideMetadata(typeof(ParsedTextBlock),
new FrameworkPropertyMetadata("No Text Set",
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender,
OnTextChanged)
);
}
private static void OnTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
ParsedTextBlock control = (ParsedTextBlock)obj;
control.Inlines.Clear();
control.Inlines.Add(new Run("test " + args.NewValue as string))
}
}