diff --git a/samples/ControlCatalog/Pages/TextBoxPage.xaml b/samples/ControlCatalog/Pages/TextBoxPage.xaml
index 0c0a4d705b..49f2eea636 100644
--- a/samples/ControlCatalog/Pages/TextBoxPage.xaml
+++ b/samples/ControlCatalog/Pages/TextBoxPage.xaml
@@ -26,6 +26,9 @@
+
diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs
index fbdf885709..94d9c2d7c5 100644
--- a/src/Avalonia.Controls/Presenters/TextPresenter.cs
+++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs
@@ -19,6 +19,12 @@ namespace Avalonia.Controls.Presenters
public static readonly StyledProperty PasswordCharProperty =
AvaloniaProperty.Register(nameof(PasswordChar));
+ public static readonly StyledProperty SelectionBrushProperty =
+ AvaloniaProperty.Register(nameof(SelectionBrushProperty));
+
+ public static readonly StyledProperty SelectionForegroundBrushProperty =
+ AvaloniaProperty.Register(nameof(SelectionForegroundBrushProperty));
+
public static readonly DirectProperty SelectionStartProperty =
TextBox.SelectionStartProperty.AddOwner(
o => o.SelectionStart,
@@ -34,7 +40,7 @@ namespace Avalonia.Controls.Presenters
private int _selectionStart;
private int _selectionEnd;
private bool _caretBlink;
- private IBrush _highlightBrush;
+ private IBrush _selectionBrush;
static TextPresenter()
{
@@ -79,6 +85,18 @@ namespace Avalonia.Controls.Presenters
set => SetValue(PasswordCharProperty, value);
}
+ public IBrush SelectionBrush
+ {
+ get => GetValue(SelectionBrushProperty);
+ set => SetValue(SelectionBrushProperty, value);
+ }
+
+ public IBrush SelectionForegroundBrush
+ {
+ get => GetValue(SelectionForegroundBrushProperty);
+ set => SetValue(SelectionForegroundBrushProperty, value);
+ }
+
public int SelectionStart
{
get
@@ -129,14 +147,11 @@ namespace Avalonia.Controls.Presenters
var rects = FormattedText.HitTestTextRange(start, length);
- if (_highlightBrush == null)
- {
- _highlightBrush = (IBrush)this.FindResource("HighlightBrush");
- }
+ _selectionBrush = SelectionBrush;
foreach (var rect in rects)
{
- context.FillRectangle(_highlightBrush, rect);
+ context.FillRectangle(_selectionBrush, rect);
}
}
@@ -247,12 +262,13 @@ namespace Avalonia.Controls.Presenters
var selectionEnd = SelectionEnd;
var start = Math.Min(selectionStart, selectionEnd);
var length = Math.Max(selectionStart, selectionEnd) - start;
+ var selectionForegroundBrush = SelectionForegroundBrush ?? Brushes.White;
if (length > 0)
{
result.Spans = new[]
{
- new FormattedTextStyleSpan(start, length, foregroundBrush: Brushes.White),
+ new FormattedTextStyleSpan(start, length, foregroundBrush: selectionForegroundBrush),
};
}
diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs
index d43957313e..0d39458a5d 100644
--- a/src/Avalonia.Controls/TextBox.cs
+++ b/src/Avalonia.Controls/TextBox.cs
@@ -38,6 +38,12 @@ namespace Avalonia.Controls
public static readonly StyledProperty PasswordCharProperty =
AvaloniaProperty.Register(nameof(PasswordChar));
+ public static readonly StyledProperty SelectionBrushProperty =
+ AvaloniaProperty.Register(nameof(SelectionBrushProperty));
+
+ public static readonly StyledProperty SelectionForegroundBrushProperty =
+ AvaloniaProperty.Register(nameof(SelectionForegroundBrushProperty));
+
public static readonly DirectProperty SelectionStartProperty =
AvaloniaProperty.RegisterDirect(
nameof(SelectionStart),
@@ -169,6 +175,18 @@ namespace Avalonia.Controls
set => SetValue(PasswordCharProperty, value);
}
+ public IBrush SelectionBrush
+ {
+ get => GetValue(SelectionBrushProperty);
+ set => SetValue(SelectionBrushProperty, value);
+ }
+
+ public IBrush SelectionForegroundBrush
+ {
+ get => GetValue(SelectionForegroundBrushProperty);
+ set => SetValue(SelectionForegroundBrushProperty, value);
+ }
+
public int SelectionStart
{
get
@@ -456,7 +474,7 @@ namespace Avalonia.Controls
movement = true;
selection = false;
handled = true;
-
+
}
else if (Match(keymap.MoveCursorToTheEndOfLine))
{
@@ -485,7 +503,7 @@ namespace Avalonia.Controls
movement = true;
selection = true;
handled = true;
-
+
}
else if (Match(keymap.MoveCursorToTheEndOfLineWithSelection))
{
diff --git a/src/Avalonia.Themes.Default/TextBox.xaml b/src/Avalonia.Themes.Default/TextBox.xaml
index 6741bdc7d9..e286e513e0 100644
--- a/src/Avalonia.Themes.Default/TextBox.xaml
+++ b/src/Avalonia.Themes.Default/TextBox.xaml
@@ -3,6 +3,7 @@
+
@@ -44,7 +45,9 @@
SelectionEnd="{TemplateBinding SelectionEnd}"
TextAlignment="{TemplateBinding TextAlignment}"
TextWrapping="{TemplateBinding TextWrapping}"
- PasswordChar="{TemplateBinding PasswordChar}"/>
+ PasswordChar="{TemplateBinding PasswordChar}"
+ SelectionBrush="{TemplateBinding SelectionBrush}"
+ SelectionForegroundBrush="{TemplateBinding SelectionForegroundBrush}"/>