InvalidOperationException when editing TextBlock.Inline from inside OnTextChanged

I get an 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)) } } 
+1
source share
1 answer

I do not believe that changing the text from the text that changed the event would be a good idea. This will cause the event to fire recursively and eventually create a stack overflow ... how ironic :)

0
source

All Articles