Browse Source
feat: Handle Datavalidations on base class "Control" (#20067 )
* Handle Datavalidations on base class "Control"
this removes the need to override it in any sub-classing control and also allowes developers to add validation support for other properties / controls by overriding Metadata
* Revert changes on Button.
The current behavior is that button gets disabled and until we decide if this is desired, keep this behavior.
pull/20156/head
Tim
2 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with
11 additions and
105 deletions
src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs
src/Avalonia.Controls/Button.cs
src/Avalonia.Controls/CalendarDatePicker/CalendarDatePicker.cs
src/Avalonia.Controls/Control.cs
src/Avalonia.Controls/DateTimePickers/DatePicker.cs
src/Avalonia.Controls/DateTimePickers/TimePicker.cs
src/Avalonia.Controls/MenuItem.cs
src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
src/Avalonia.Controls/Slider.cs
src/Avalonia.Controls/TextBox.cs
@ -652,24 +652,6 @@ namespace Avalonia.Controls
base . OnApplyTemplate ( e ) ;
base . OnApplyTemplate ( e ) ;
}
}
/// <summary>
/// Called to update the validation state for properties for which data validation is
/// enabled.
/// </summary>
/// <param name="property">The property.</param>
/// <param name="state">The current data binding state.</param>
/// <param name="error">The current data binding error, if any.</param>
protected override void UpdateDataValidation (
AvaloniaProperty property ,
BindingValueType state ,
Exception ? error )
{
if ( property = = TextProperty | | property = = SelectedItemProperty )
{
DataValidationErrors . SetError ( this , error ) ;
}
}
/// <summary>
/// <summary>
/// Provides handling for the
/// Provides handling for the
/// <see cref="E:Avalonia.InputElement.KeyDown" /> event.
/// <see cref="E:Avalonia.InputElement.KeyDown" /> event.
@ -1,4 +1,5 @@
using System ;
using System ;
using System.Diagnostics ;
using System.Linq ;
using System.Linq ;
using System.Windows.Input ;
using System.Windows.Input ;
using Avalonia.Automation.Peers ;
using Avalonia.Automation.Peers ;
@ -571,7 +572,7 @@ namespace Avalonia.Controls
}
}
}
}
}
}
internal void PerformClick ( ) = > OnClick ( ) ;
internal void PerformClick ( ) = > OnClick ( ) ;
private static void OnAccessKeyPressed ( Button sender , AccessKeyPressedEventArgs e )
private static void OnAccessKeyPressed ( Button sender , AccessKeyPressedEventArgs e )
@ -325,17 +325,6 @@ namespace Avalonia.Controls
base . OnPropertyChanged ( change ) ;
base . OnPropertyChanged ( change ) ;
}
}
/// <inheritdoc/>
protected override void UpdateDataValidation ( AvaloniaProperty property , BindingValueType state , Exception ? error )
{
if ( property = = SelectedDateProperty )
{
DataValidationErrors . SetError ( this , error ) ;
}
base . UpdateDataValidation ( property , state , error ) ;
}
/// <inheritdoc/>
/// <inheritdoc/>
protected override void OnPointerPressed ( PointerPressedEventArgs e )
protected override void OnPointerPressed ( PointerPressedEventArgs e )
{
{
@ -4,6 +4,7 @@ using System.ComponentModel;
using Avalonia.Automation.Peers ;
using Avalonia.Automation.Peers ;
using Avalonia.Controls.Primitives ;
using Avalonia.Controls.Primitives ;
using Avalonia.Controls.Templates ;
using Avalonia.Controls.Templates ;
using Avalonia.Data ;
using Avalonia.Input ;
using Avalonia.Input ;
using Avalonia.Input.Platform ;
using Avalonia.Input.Platform ;
using Avalonia.Interactivity ;
using Avalonia.Interactivity ;
@ -563,6 +564,13 @@ namespace Avalonia.Controls
}
}
}
}
}
}
/// <inheritdoc />
protected override void UpdateDataValidation ( AvaloniaProperty property , BindingValueType state , Exception ? error )
{
DataValidationErrors . SetError ( this , error ) ;
base . UpdateDataValidation ( property , state , error ) ;
}
// Since we are resetting the dispatcher instance, the callback might never arrive
// Since we are resetting the dispatcher instance, the callback might never arrive
internal static void ResetLoadedQueueForUnitTests ( )
internal static void ResetLoadedQueueForUnitTests ( )
@ -426,13 +426,5 @@ namespace Avalonia.Controls
{
{
SetCurrentValue ( SelectedDateProperty , null ) ;
SetCurrentValue ( SelectedDateProperty , null ) ;
}
}
protected override void UpdateDataValidation ( AvaloniaProperty property , BindingValueType state , Exception ? error )
{
base . UpdateDataValidation ( property , state , error ) ;
if ( property = = SelectedDateProperty )
DataValidationErrors . SetError ( this , error ) ;
}
}
}
}
}
@ -407,13 +407,5 @@ namespace Avalonia.Controls
{
{
SetCurrentValue ( SelectedTimeProperty , null ) ;
SetCurrentValue ( SelectedTimeProperty , null ) ;
}
}
protected override void UpdateDataValidation ( AvaloniaProperty property , BindingValueType state , Exception ? error )
{
base . UpdateDataValidation ( property , state , error ) ;
if ( property = = SelectedTimeProperty )
DataValidationErrors . SetError ( this , error ) ;
}
}
}
}
}
@ -554,6 +554,7 @@ namespace Avalonia.Controls
return new MenuItemAutomationPeer ( this ) ;
return new MenuItemAutomationPeer ( this ) ;
}
}
// TODO: This is confusing for some ppl. Need to think about alternatives here.
protected override void UpdateDataValidation (
protected override void UpdateDataValidation (
AvaloniaProperty property ,
AvaloniaProperty property ,
BindingValueType state ,
BindingValueType state ,
@ -441,24 +441,6 @@ namespace Avalonia.Controls
}
}
}
}
/// <summary>
/// Called to update the validation state for properties for which data validation is
/// enabled.
/// </summary>
/// <param name="property">The property.</param>
/// <param name="state">The current data binding state.</param>
/// <param name="error">The current data binding error, if any.</param>
protected override void UpdateDataValidation (
AvaloniaProperty property ,
BindingValueType state ,
Exception ? error )
{
if ( property = = TextProperty | | property = = ValueProperty )
{
DataValidationErrors . SetError ( this , error ) ;
}
}
/// <summary>
/// <summary>
/// Called when the <see cref="NumberFormat"/> property value changed.
/// Called when the <see cref="NumberFormat"/> property value changed.
/// </summary>
/// </summary>
@ -569,24 +569,6 @@ namespace Avalonia.Controls.Primitives
EndUpdating ( ) ;
EndUpdating ( ) ;
}
}
/// <summary>
/// Called to update the validation state for properties for which data validation is
/// enabled.
/// </summary>
/// <param name="property">The property.</param>
/// <param name="state">The current data binding state.</param>
/// <param name="error">The current data binding error, if any.</param>
protected override void UpdateDataValidation (
AvaloniaProperty property ,
BindingValueType state ,
Exception ? error )
{
if ( property = = SelectedItemProperty )
{
DataValidationErrors . SetError ( this , error ) ;
}
}
/// <inheritdoc />
/// <inheritdoc />
protected override void OnInitialized ( )
protected override void OnInitialized ( )
{
{
@ -393,18 +393,6 @@ namespace Avalonia.Controls
SetCurrentValue ( ValueProperty , IsSnapToTickEnabled ? SnapToTick ( finalValue ) : finalValue ) ;
SetCurrentValue ( ValueProperty , IsSnapToTickEnabled ? SnapToTick ( finalValue ) : finalValue ) ;
}
}
/// <inheritdoc />
protected override void UpdateDataValidation (
AvaloniaProperty property ,
BindingValueType state ,
Exception ? error )
{
if ( property = = ValueProperty )
{
DataValidationErrors . SetError ( this , error ) ;
}
}
protected override AutomationPeer OnCreateAutomationPeer ( )
protected override AutomationPeer OnCreateAutomationPeer ( )
{
{
return new SliderAutomationPeer ( this ) ;
return new SliderAutomationPeer ( this ) ;
@ -1884,17 +1884,6 @@ namespace Avalonia.Controls
return new TextBoxAutomationPeer ( this ) ;
return new TextBoxAutomationPeer ( this ) ;
}
}
protected override void UpdateDataValidation (
AvaloniaProperty property ,
BindingValueType state ,
Exception ? error )
{
if ( property = = TextProperty )
{
DataValidationErrors . SetError ( this , error ) ;
}
}
internal static int CoerceCaretIndex ( AvaloniaObject sender , int value )
internal static int CoerceCaretIndex ( AvaloniaObject sender , int value )
{
{
var text = sender . GetValue ( TextProperty ) ; // method also used by TextPresenter and SelectableTextBlock
var text = sender . GetValue ( TextProperty ) ; // method also used by TextPresenter and SelectableTextBlock