Skip to content

Commit

Permalink
Merge pull request #12624 from Actipro/fix-text-decorations
Browse files Browse the repository at this point in the history
Fix for TextBlock.TextDecorations not inheriting down to inlines.
  • Loading branch information
maxkatz6 authored and grokys committed Oct 2, 2023
1 parent f585ddd commit a5a69f5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/Avalonia.Controls/Documents/Inline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ public abstract class Inline : TextElement
/// <summary>
/// AvaloniaProperty for <see cref="TextDecorations" /> property.
/// </summary>
public static readonly StyledProperty<TextDecorationCollection?> TextDecorationsProperty =
AvaloniaProperty.Register<Inline, TextDecorationCollection?>(
nameof(TextDecorations));
public static readonly AttachedProperty<TextDecorationCollection?> TextDecorationsProperty =
AvaloniaProperty.RegisterAttached<Inline, Inline, TextDecorationCollection?>(
nameof(TextDecorations),
inherits: true);

/// <summary>
/// AvaloniaProperty for <see cref="BaselineAlignment" /> property.
Expand Down Expand Up @@ -43,7 +44,27 @@ public BaselineAlignment BaselineAlignment
get { return GetValue(BaselineAlignmentProperty); }
set { SetValue(BaselineAlignmentProperty, value); }
}

/// <summary>
/// Gets the value of the attached <see cref="TextDecorationsProperty"/> on a control.
/// </summary>
/// <param name="control">The control.</param>
/// <returns>The font style.</returns>
public static TextDecorationCollection? GetTextDecorations(Control control)
{
return control.GetValue(TextDecorationsProperty);
}

/// <summary>
/// Sets the value of the attached <see cref="TextDecorationsProperty"/> on a control.
/// </summary>
/// <param name="control">The control.</param>
/// <param name="value">The property value to set.</param>
public static void SetTextDecorations(Control control, TextDecorationCollection? value)
{
control.SetValue(TextDecorationsProperty, value);
}

internal abstract void BuildTextRun(IList<TextRun> textRuns);

internal abstract void AppendText(StringBuilder stringBuilder);
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Controls/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public class TextBlock : Control, IInlineHost
/// Defines the <see cref="TextDecorations"/> property.
/// </summary>
public static readonly StyledProperty<TextDecorationCollection?> TextDecorationsProperty =
AvaloniaProperty.Register<TextBlock, TextDecorationCollection?>(nameof(TextDecorations));
Inline.TextDecorationsProperty.AddOwner<TextBlock>();

/// <summary>
/// Defines the <see cref="Inlines"/> property.
Expand Down
21 changes: 21 additions & 0 deletions tests/Avalonia.Controls.UnitTests/TextBlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,26 @@ public void Setting_Text_Should_Reset_Inlines()
Assert.Equal(0, target.Inlines.Count);
}
}

[Fact]
public void Setting_TextDecorations_Should_Update_Inlines()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var target = new TextBlock();

target.Inlines.Add(new Run("Hello World"));

Assert.Equal(1, target.Inlines.Count);

Assert.Null(target.Inlines[0].TextDecorations);

var underline = TextDecorations.Underline;

target.TextDecorations = underline;

Assert.Equal(underline, target.Inlines[0].TextDecorations);
}
}
}
}

0 comments on commit a5a69f5

Please sign in to comment.