Skip to content

Fix for TextBlock.TextDecorations not inheriting down to inlines. #12624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}