diff --git a/src/Avalonia.Controls/Documents/InlineCollection.cs b/src/Avalonia.Controls/Documents/InlineCollection.cs index a265f88e21..0329429051 100644 --- a/src/Avalonia.Controls/Documents/InlineCollection.cs +++ b/src/Avalonia.Controls/Documents/InlineCollection.cs @@ -89,55 +89,49 @@ namespace Avalonia.Controls.Documents } + public override void Add(Inline inline) + { + if (InlineHost is TextBlock textBlock && !string.IsNullOrEmpty(textBlock._text)) + { + base.Add(new Run(textBlock._text)); + + textBlock._text = null; + } + + base.Add(inline); + } + /// - /// Add a text segment to the collection. + /// Adds a text segment to the collection. /// /// For non complex content this appends the text to the end of currently held text. /// For complex content this adds a to the collection. /// /// - /// + /// The to be added text. public void Add(string text) { AddText(text); } - public override void Add(Inline inline) - { - OnAdd(); - - base.Add(inline); - } - - public void Add(IControl child) + /// + /// Adds a control wrapped inside a to the collection. + /// + /// The to be added control. + public void Add(IControl control) { - OnAdd(); - - base.Add(new InlineUIContainer(child)); + Add(new InlineUIContainer(control)); } - private void AddText(string text) + internal void AddText(string text) { - if (LogicalChildren is TextBlock textBlock && !textBlock.HasComplexContent) + if (InlineHost is TextBlock textBlock && !textBlock.HasComplexContent) { textBlock._text += text; } else { - base.Add(new Run(text)); - } - } - - private void OnAdd() - { - if (LogicalChildren is TextBlock textBlock) - { - if (!textBlock.HasComplexContent && !string.IsNullOrEmpty(textBlock._text)) - { - base.Add(new Run(textBlock._text)); - - textBlock._text = null; - } + Add(new Run(text)); } } diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 0492c2c1e3..c8e05e5cb3 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -597,23 +597,12 @@ namespace Avalonia.Controls protected virtual void SetText(string? text) { - if (Inlines != null && Inlines.Count > 0) - { - var oldValue = Inlines.Text; - - if (!string.IsNullOrEmpty(text)) - { - Inlines.Add(text); - } - - text = Inlines.Text; - - RaisePropertyChanged(TextProperty, oldValue, text); - } - else + if (HasComplexContent) { - SetAndRaise(TextProperty, ref _text, text); + Inlines?.Clear(); } + + SetAndRaise(TextProperty, ref _text, text); } ///