Browse Source

Add TextBox.TextChanging event

pull/9157/head
robloo 4 years ago
parent
commit
d3dcf73113
  1. 50
      src/Avalonia.Controls/TextBox.cs

50
src/Avalonia.Controls/TextBox.cs

@ -160,22 +160,41 @@ namespace Avalonia.Controls
(o, v) => o.UndoLimit = v, (o, v) => o.UndoLimit = v,
unsetValue: -1); unsetValue: -1);
/// <summary>
/// Defines the <see cref="CopyingToClipboard"/> event.
/// </summary>
public static readonly RoutedEvent<RoutedEventArgs> CopyingToClipboardEvent = public static readonly RoutedEvent<RoutedEventArgs> CopyingToClipboardEvent =
RoutedEvent.Register<TextBox, RoutedEventArgs>( RoutedEvent.Register<TextBox, RoutedEventArgs>(
nameof(CopyingToClipboard), RoutingStrategies.Bubble); nameof(CopyingToClipboard), RoutingStrategies.Bubble);
/// <summary>
/// Defines the <see cref="CuttingToClipboard"/> event.
/// </summary>
public static readonly RoutedEvent<RoutedEventArgs> CuttingToClipboardEvent = public static readonly RoutedEvent<RoutedEventArgs> CuttingToClipboardEvent =
RoutedEvent.Register<TextBox, RoutedEventArgs>( RoutedEvent.Register<TextBox, RoutedEventArgs>(
nameof(CuttingToClipboard), RoutingStrategies.Bubble); nameof(CuttingToClipboard), RoutingStrategies.Bubble);
/// <summary>
/// Defines the <see cref="PastingFromClipboard"/> event.
/// </summary>
public static readonly RoutedEvent<RoutedEventArgs> PastingFromClipboardEvent = public static readonly RoutedEvent<RoutedEventArgs> PastingFromClipboardEvent =
RoutedEvent.Register<TextBox, RoutedEventArgs>( RoutedEvent.Register<TextBox, RoutedEventArgs>(
nameof(PastingFromClipboard), RoutingStrategies.Bubble); nameof(PastingFromClipboard), RoutingStrategies.Bubble);
/// <summary>
/// Defines the <see cref="TextChanged"/> event.
/// </summary>
public static readonly RoutedEvent<RoutedEventArgs> TextChangedEvent = public static readonly RoutedEvent<RoutedEventArgs> TextChangedEvent =
RoutedEvent.Register<TextBox, RoutedEventArgs>( RoutedEvent.Register<TextBox, RoutedEventArgs>(
nameof(TextChanged), RoutingStrategies.Bubble); nameof(TextChanged), RoutingStrategies.Bubble);
/// <summary>
/// Defines the <see cref="TextChanging"/> event.
/// </summary>
public static readonly RoutedEvent<RoutedEventArgs> TextChangingEvent =
RoutedEvent.Register<TextBox, RoutedEventArgs>(
nameof(TextChanging), RoutingStrategies.Bubble);
readonly struct UndoRedoState : IEquatable<UndoRedoState> readonly struct UndoRedoState : IEquatable<UndoRedoState>
{ {
public string? Text { get; } public string? Text { get; }
@ -570,17 +589,26 @@ namespace Avalonia.Controls
} }
/// <summary> /// <summary>
/// Occurs when text changes. /// Occurs asynchronously after text changes and the new text is rendered.
/// </summary> /// </summary>
/// <remarks>
/// This event is asynchronous and occurs after the new text is rendered.
/// </remarks>
public event EventHandler<RoutedEventArgs>? TextChanged public event EventHandler<RoutedEventArgs>? TextChanged
{ {
add => AddHandler(TextChangedEvent, value); add => AddHandler(TextChangedEvent, value);
remove => RemoveHandler(TextChangedEvent, value); remove => RemoveHandler(TextChangedEvent, value);
} }
/// <summary>
/// Occurs synchronously when text starts to change but before it is rendered.
/// </summary>
/// <remarks>
/// This event occurs just after the <see cref="Text"/> property value has been updated.
/// </remarks>
public event EventHandler<RoutedEventArgs>? TextChanging
{
add => AddHandler(TextChangingEvent, value);
remove => RemoveHandler(TextChangingEvent, value);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
_presenter = e.NameScope.Get<TextPresenter>("PART_TextPresenter"); _presenter = e.NameScope.Get<TextPresenter>("PART_TextPresenter");
@ -1564,11 +1592,19 @@ namespace Avalonia.Controls
if (textChanged) if (textChanged)
{ {
// Note the following sequence of these events (following WinUI)
// 1. TextChanging occurs synchronously when text starts to change but before it is rendered.
// This occurs after the Text property is set.
// 2. TextChanged occurs asynchronously after text changes and the new text is rendered.
var textChangingEventArgs = new RoutedEventArgs(TextChangingEvent);
RaiseEvent(textChangingEventArgs);
Dispatcher.UIThread.Post(() => Dispatcher.UIThread.Post(() =>
{ {
var eventArgs = new RoutedEventArgs(TextChangedEvent); var textChangedEventArgs = new RoutedEventArgs(TextChangedEvent);
RaiseEvent(eventArgs); RaiseEvent(textChangedEventArgs);
}); }, DispatcherPriority.Normal);
} }
} }
else else

Loading…
Cancel
Save