I'm getting InvalidOperationException somewhat randomly in the following code, what's a good way to fix it?
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))
}
}