Browse Source

Fix SelectableTextBlock selection

pull/10821/head
Benedikt Stebner 3 years ago
parent
commit
45bf804cd4
  1. 15
      src/Avalonia.Controls/SelectableTextBlock.cs

15
src/Avalonia.Controls/SelectableTextBlock.cs

@ -18,10 +18,10 @@ namespace Avalonia.Controls
public class SelectableTextBlock : TextBlock, IInlineHost
{
public static readonly StyledProperty<int> SelectionStartProperty =
TextBox.SelectionStartProperty.AddOwner<SelectableTextBlock>(new(coerce: TextBox.CoerceCaretIndex));
TextBox.SelectionStartProperty.AddOwner<SelectableTextBlock>();
public static readonly StyledProperty<int> SelectionEndProperty =
TextBox.SelectionEndProperty.AddOwner<SelectableTextBlock>(new(coerce: TextBox.CoerceCaretIndex));
TextBox.SelectionEndProperty.AddOwner<SelectableTextBlock>();
public static readonly DirectProperty<SelectableTextBlock, string> SelectedTextProperty =
AvaloniaProperty.RegisterDirect<SelectableTextBlock, string>(
@ -228,7 +228,7 @@ namespace Avalonia.Controls
{
base.OnPointerPressed(e);
var text = Text;
var text = HasComplexContent ? Inlines.Text : Text;
var clickInfo = e.GetCurrentPoint(this);
if (text != null && clickInfo.Properties.IsLeftButtonPressed)
@ -314,7 +314,7 @@ namespace Avalonia.Controls
// selection should not change during pointer move if the user right clicks
if (e.Pointer.Captured == this && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
var text = Text;
var text = HasComplexContent ? Inlines.Text : Text;
var padding = Padding;
var point = e.GetPosition(this) - new Point(padding.Left, padding.Top);
@ -397,7 +397,10 @@ namespace Avalonia.Controls
private string GetSelection()
{
var textLength = Text?.Length ?? 0;
var text = HasComplexContent ? Inlines.Text : Text;
var textLength = text?.Length ?? 0;
if (textLength == 0)
{
return "";
@ -415,7 +418,7 @@ namespace Avalonia.Controls
var length = Math.Max(0, end - start);
var selectedText = Text!.Substring(start, length);
var selectedText = text!.Substring(start, length);
return selectedText;
}

Loading…
Cancel
Save