Skip to content

Commit 412438d

Browse files
authored
Merge pull request #12624 from Actipro/fix-text-decorations
Fix for TextBlock.TextDecorations not inheriting down to inlines.
2 parents 5539f01 + 16ab9c5 commit 412438d

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/Avalonia.Controls/Documents/Inline.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ public abstract class Inline : TextElement
1313
/// <summary>
1414
/// AvaloniaProperty for <see cref="TextDecorations" /> property.
1515
/// </summary>
16-
public static readonly StyledProperty<TextDecorationCollection?> TextDecorationsProperty =
17-
AvaloniaProperty.Register<Inline, TextDecorationCollection?>(
18-
nameof(TextDecorations));
16+
public static readonly AttachedProperty<TextDecorationCollection?> TextDecorationsProperty =
17+
AvaloniaProperty.RegisterAttached<Inline, Inline, TextDecorationCollection?>(
18+
nameof(TextDecorations),
19+
inherits: true);
1920

2021
/// <summary>
2122
/// AvaloniaProperty for <see cref="BaselineAlignment" /> property.
@@ -43,7 +44,27 @@ public BaselineAlignment BaselineAlignment
4344
get { return GetValue(BaselineAlignmentProperty); }
4445
set { SetValue(BaselineAlignmentProperty, value); }
4546
}
47+
48+
/// <summary>
49+
/// Gets the value of the attached <see cref="TextDecorationsProperty"/> on a control.
50+
/// </summary>
51+
/// <param name="control">The control.</param>
52+
/// <returns>The font style.</returns>
53+
public static TextDecorationCollection? GetTextDecorations(Control control)
54+
{
55+
return control.GetValue(TextDecorationsProperty);
56+
}
4657

58+
/// <summary>
59+
/// Sets the value of the attached <see cref="TextDecorationsProperty"/> on a control.
60+
/// </summary>
61+
/// <param name="control">The control.</param>
62+
/// <param name="value">The property value to set.</param>
63+
public static void SetTextDecorations(Control control, TextDecorationCollection? value)
64+
{
65+
control.SetValue(TextDecorationsProperty, value);
66+
}
67+
4768
internal abstract void BuildTextRun(IList<TextRun> textRuns);
4869

4970
internal abstract void AppendText(StringBuilder stringBuilder);

src/Avalonia.Controls/TextBlock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public class TextBlock : Control, IInlineHost
135135
/// Defines the <see cref="TextDecorations"/> property.
136136
/// </summary>
137137
public static readonly StyledProperty<TextDecorationCollection?> TextDecorationsProperty =
138-
AvaloniaProperty.Register<TextBlock, TextDecorationCollection?>(nameof(TextDecorations));
138+
Inline.TextDecorationsProperty.AddOwner<TextBlock>();
139139

140140
/// <summary>
141141
/// Defines the <see cref="Inlines"/> property.

tests/Avalonia.Controls.UnitTests/TextBlockTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,26 @@ public void Setting_Text_Should_Reset_Inlines()
202202
Assert.Equal(0, target.Inlines.Count);
203203
}
204204
}
205+
206+
[Fact]
207+
public void Setting_TextDecorations_Should_Update_Inlines()
208+
{
209+
using (UnitTestApplication.Start(TestServices.StyledWindow))
210+
{
211+
var target = new TextBlock();
212+
213+
target.Inlines.Add(new Run("Hello World"));
214+
215+
Assert.Equal(1, target.Inlines.Count);
216+
217+
Assert.Null(target.Inlines[0].TextDecorations);
218+
219+
var underline = TextDecorations.Underline;
220+
221+
target.TextDecorations = underline;
222+
223+
Assert.Equal(underline, target.Inlines[0].TextDecorations);
224+
}
225+
}
205226
}
206227
}

0 commit comments

Comments
 (0)