mirror of
https://github.com/AtomUI/AtomUI.git
synced 2026-10-29 07:16:33 +08:00
Add DataGrid EventArgs
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridRowGroupHeader : TemplatedControl
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridAutoGeneratingColumnEventArgs : CancelEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="T:Avalonia.Controls.DataGridAutoGeneratingColumnEventArgs" /> class.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">
|
||||
/// The name of the property bound to the generated column.
|
||||
/// </param>
|
||||
/// <param name="propertyType">
|
||||
/// The <see cref="T:System.Type" /> of the property bound to the generated column.
|
||||
/// </param>
|
||||
/// <param name="column">
|
||||
/// The generated column.
|
||||
/// </param>
|
||||
public DataGridAutoGeneratingColumnEventArgs(string propertyName, Type propertyType, DataGridColumn column)
|
||||
{
|
||||
Column = column;
|
||||
PropertyName = propertyName;
|
||||
PropertyType = propertyType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the generated column.
|
||||
/// </summary>
|
||||
public DataGridColumn Column
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the property bound to the generated column.
|
||||
/// </summary>
|
||||
public string PropertyName
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="T:System.Type" /> of the property bound to the generated column.
|
||||
/// </summary>
|
||||
public Type PropertyType
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
using System.ComponentModel;
|
||||
using Avalonia.Interactivity;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridBeginningEditEventArgs: CancelEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the
|
||||
/// <see cref="T:Avalonia.Controls.DataGridBeginningEditEventArgs" /> class.
|
||||
/// </summary>
|
||||
/// <param name="column">
|
||||
/// The column that contains the cell to be edited.
|
||||
/// </param>
|
||||
/// <param name="row">
|
||||
/// The row that contains the cell to be edited.
|
||||
/// </param>
|
||||
/// <param name="editingEventArgs">
|
||||
/// Information about the user gesture that caused the cell to enter edit mode.
|
||||
/// </param>
|
||||
public DataGridBeginningEditEventArgs(DataGridColumn column,
|
||||
DataGridRow row,
|
||||
RoutedEventArgs editingEventArgs)
|
||||
{
|
||||
Column = column;
|
||||
Row = row;
|
||||
EditingEventArgs = editingEventArgs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the column that contains the cell to be edited.
|
||||
/// </summary>
|
||||
public DataGridColumn Column
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets information about the user gesture that caused the cell to enter edit mode.
|
||||
/// </summary>
|
||||
public RoutedEventArgs EditingEventArgs
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the row that contains the cell to be edited.
|
||||
/// </summary>
|
||||
public DataGridRow Row
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridCellEditEndedEventArgs : System.EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Instantiates a new instance of this class.
|
||||
/// </summary>
|
||||
/// <param name="column">The column of the cell that has just exited edit mode.</param>
|
||||
/// <param name="row">The row container of the cell container that has just exited edit mode.</param>
|
||||
/// <param name="editAction">The editing action that has been taken.</param>
|
||||
public DataGridCellEditEndedEventArgs(DataGridColumn column, DataGridRow row, DataGridEditAction editAction)
|
||||
{
|
||||
Column = column;
|
||||
Row = row;
|
||||
EditAction = editAction;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The column of the cell that has just exited edit mode.
|
||||
/// </summary>
|
||||
public DataGridColumn Column
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The edit action taken when leaving edit mode.
|
||||
/// </summary>
|
||||
public DataGridEditAction EditAction
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The row container of the cell container that has just exited edit mode.
|
||||
/// </summary>
|
||||
public DataGridRow Row
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
using System.ComponentModel;
|
||||
using Avalonia.Controls;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridCellEditEndingEventArgs : CancelEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Instantiates a new instance of this class.
|
||||
/// </summary>
|
||||
/// <param name="column">The column of the cell that is about to exit edit mode.</param>
|
||||
/// <param name="row">The row container of the cell container that is about to exit edit mode.</param>
|
||||
/// <param name="editingElement">The editing element within the cell.</param>
|
||||
/// <param name="editAction">The editing action that will be taken.</param>
|
||||
public DataGridCellEditEndingEventArgs(DataGridColumn column,
|
||||
DataGridRow row,
|
||||
Control editingElement,
|
||||
DataGridEditAction editAction)
|
||||
{
|
||||
Column = column;
|
||||
Row = row;
|
||||
EditingElement = editingElement;
|
||||
EditAction = editAction;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The column of the cell that is about to exit edit mode.
|
||||
/// </summary>
|
||||
public DataGridColumn Column
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The edit action to take when leaving edit mode.
|
||||
/// </summary>
|
||||
public DataGridEditAction EditAction
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The editing element within the cell.
|
||||
/// </summary>
|
||||
public Control EditingElement
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The row container of the cell container that is about to exit edit mode.
|
||||
/// </summary>
|
||||
public DataGridRow Row
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridCellEventArgs : EventArgs
|
||||
{
|
||||
internal DataGridCellEventArgs(DataGridCell dataGridCell)
|
||||
{
|
||||
Debug.Assert(dataGridCell != null);
|
||||
Cell = dataGridCell;
|
||||
}
|
||||
|
||||
internal DataGridCell Cell
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
using Avalonia.Input;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridCellPointerPressedEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Instantiates a new instance of this class.
|
||||
/// </summary>
|
||||
/// <param name="cell">The cell that has been pressed.</param>
|
||||
/// <param name="row">The row container of the cell that has been pressed.</param>
|
||||
/// <param name="column">The column of the cell that has been pressed.</param>
|
||||
/// <param name="e">The pointer action that has been taken.</param>
|
||||
public DataGridCellPointerPressedEventArgs(DataGridCell cell,
|
||||
DataGridRow row,
|
||||
DataGridColumn column,
|
||||
PointerPressedEventArgs e)
|
||||
{
|
||||
Cell = cell;
|
||||
Row = row;
|
||||
Column = column;
|
||||
PointerPressedEventArgs = e;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The cell that has been pressed.
|
||||
/// </summary>
|
||||
public DataGridCell Cell { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The row container of the cell that has been pressed.
|
||||
/// </summary>
|
||||
public DataGridRow Row { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The column of the cell that has been pressed.
|
||||
/// </summary>
|
||||
public DataGridColumn Column { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The pointer action that has been taken.
|
||||
/// </summary>
|
||||
public PointerPressedEventArgs PointerPressedEventArgs { get; }
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridColumnEventArgs : HandledEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="T:Avalonia.Controls.DataGridColumnEventArgs" /> class.
|
||||
/// </summary>
|
||||
/// <param name="column">The column that the event occurs for.</param>
|
||||
public DataGridColumnEventArgs(DataGridColumn column)
|
||||
{
|
||||
Column = column ?? throw new ArgumentNullException(nameof(column));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the column that the event occurs for.
|
||||
/// </summary>
|
||||
public DataGridColumn Column
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
using System.ComponentModel;
|
||||
using Avalonia.Controls;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridColumnReorderingEventArgs : CancelEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="T:Avalonia.Controls.DataGridColumnReorderingEventArgs" /> class.
|
||||
/// </summary>
|
||||
/// <param name="dataGridColumn"></param>
|
||||
public DataGridColumnReorderingEventArgs(DataGridColumn dataGridColumn)
|
||||
{
|
||||
Column = dataGridColumn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The column being moved.
|
||||
/// </summary>
|
||||
public DataGridColumn Column
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The popup indicator displayed while dragging. If null and Handled = true, then do not display a tooltip.
|
||||
/// </summary>
|
||||
public Control? DragIndicator
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UIElement to display at the insertion position. If null and Handled = true, then do not display an insertion indicator.
|
||||
/// </summary>
|
||||
public Control? DropLocationIndicator
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridPreparingCellForEditEventArgs: EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="T:Avalonia.Controls.DataGridPreparingCellForEditEventArgs" /> class.
|
||||
/// </summary>
|
||||
/// <param name="column">The column that contains the cell to be edited.</param>
|
||||
/// <param name="row">The row that contains the cell to be edited.</param>
|
||||
/// <param name="editingEventArgs">Information about the user gesture that caused the cell to enter edit mode.</param>
|
||||
/// <param name="editingElement">The element that the column displays for a cell in editing mode.</param>
|
||||
public DataGridPreparingCellForEditEventArgs(DataGridColumn column,
|
||||
DataGridRow row,
|
||||
RoutedEventArgs editingEventArgs,
|
||||
Control editingElement)
|
||||
{
|
||||
Column = column;
|
||||
Row = row;
|
||||
EditingEventArgs = editingEventArgs;
|
||||
EditingElement = editingElement;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the column that contains the cell to be edited.
|
||||
/// </summary>
|
||||
public DataGridColumn Column
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the element that the column displays for a cell in editing mode.
|
||||
/// </summary>
|
||||
public Control EditingElement
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets information about the user gesture that caused the cell to enter edit mode.
|
||||
/// </summary>
|
||||
public RoutedEventArgs EditingEventArgs
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the row that contains the cell to be edited.
|
||||
/// </summary>
|
||||
public DataGridRow Row
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
using Avalonia.Controls;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridRowDetailsEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="T:Avalonia.Controls.DataGridRowDetailsEventArgs" /> class.
|
||||
/// </summary>
|
||||
/// <param name="row">The row that the event occurs for.</param>
|
||||
/// <param name="detailsElement">The row details section as a framework element.</param>
|
||||
public DataGridRowDetailsEventArgs(DataGridRow row, Control detailsElement)
|
||||
{
|
||||
Row = row;
|
||||
DetailsElement = detailsElement;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the row details section as a framework element.
|
||||
/// </summary>
|
||||
public Control DetailsElement
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the row that the event occurs for.
|
||||
/// </summary>
|
||||
public DataGridRow Row
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridRowEditEndedEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Instantiates a new instance of this class.
|
||||
/// </summary>
|
||||
/// <param name="row">The row container of the cell container that has just exited edit mode.</param>
|
||||
/// <param name="editAction">The editing action that has been taken.</param>
|
||||
public DataGridRowEditEndedEventArgs(DataGridRow row, DataGridEditAction editAction)
|
||||
{
|
||||
Row = row;
|
||||
EditAction = editAction;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The editing action that has been taken.
|
||||
/// </summary>
|
||||
public DataGridEditAction EditAction
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The row container of the cell container that has just exited edit mode.
|
||||
/// </summary>
|
||||
public DataGridRow Row
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridRowEditEndingEventArgs : CancelEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Instantiates a new instance of this class.
|
||||
/// </summary>
|
||||
/// <param name="row">The row container of the cell container that is about to exit edit mode.</param>
|
||||
/// <param name="editAction">The editing action that will be taken.</param>
|
||||
public DataGridRowEditEndingEventArgs(DataGridRow row, DataGridEditAction editAction)
|
||||
{
|
||||
Row = row;
|
||||
EditAction = editAction;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The editing action that will be taken.
|
||||
/// </summary>
|
||||
public DataGridEditAction EditAction
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The row container of the cell container that is about to exit edit mode.
|
||||
/// </summary>
|
||||
public DataGridRow Row
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridRowEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="T:Avalonia.Controls.DataGridRowEventArgs" /> class.
|
||||
/// </summary>
|
||||
/// <param name="dataGridRow">The row that the event occurs for.</param>
|
||||
public DataGridRowEventArgs(DataGridRow dataGridRow)
|
||||
{
|
||||
Row = dataGridRow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the row that the event occurs for.
|
||||
/// </summary>
|
||||
public DataGridRow Row
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// (c) Copyright Microsoft Corporation.
|
||||
// This source is subject to the Microsoft Public License (Ms-PL).
|
||||
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
|
||||
// All other rights reserved.
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
public class DataGridRowGroupHeaderEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructs a DataGridRowGroupHeaderEventArgs instance
|
||||
/// </summary>
|
||||
/// <param name="rowGroupHeader"></param>
|
||||
public DataGridRowGroupHeaderEventArgs(DataGridRowGroupHeader rowGroupHeader)
|
||||
{
|
||||
RowGroupHeader = rowGroupHeader;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DataGridRowGroupHeader associated with this instance
|
||||
/// </summary>
|
||||
public DataGridRowGroupHeader RowGroupHeader
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Avalonia.Data;
|
||||
|
||||
namespace AtomUI.Controls.Utils;
|
||||
|
||||
internal static class ValidationUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Searches a ValidationResult for the specified target member name. If the target is null
|
||||
/// or empty, this method will return true if there are no member names at all.
|
||||
/// </summary>
|
||||
/// <param name="validationResult">ValidationResult to search.</param>
|
||||
/// <param name="target">Member name to search for.</param>
|
||||
/// <returns>True if found.</returns>
|
||||
public static bool ContainsMemberName(this ValidationResult validationResult, string target)
|
||||
{
|
||||
int memberNameCount = 0;
|
||||
foreach (string memberName in validationResult.MemberNames)
|
||||
{
|
||||
if (string.Equals(target, memberName))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
memberNameCount++;
|
||||
}
|
||||
return (memberNameCount == 0 && string.IsNullOrEmpty(target));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds an equivalent ValidationResult if one exists.
|
||||
/// </summary>
|
||||
/// <param name="collection">ValidationResults to search through.</param>
|
||||
/// <param name="target">ValidationResult to find.</param>
|
||||
/// <returns>Equal ValidationResult if found, null otherwise.</returns>
|
||||
public static ValidationResult? FindEqualValidationResult(this ICollection<ValidationResult> collection, ValidationResult target)
|
||||
{
|
||||
foreach (ValidationResult oldValidationResult in collection)
|
||||
{
|
||||
if (oldValidationResult.ErrorMessage == target.ErrorMessage)
|
||||
{
|
||||
bool movedOld = true;
|
||||
bool movedTarget = true;
|
||||
IEnumerator<string> oldEnumerator = oldValidationResult.MemberNames.GetEnumerator();
|
||||
IEnumerator<string> targetEnumerator = target.MemberNames.GetEnumerator();
|
||||
while (movedOld && movedTarget)
|
||||
{
|
||||
movedOld = oldEnumerator.MoveNext();
|
||||
movedTarget = targetEnumerator.MoveNext();
|
||||
|
||||
if (!movedOld && !movedTarget)
|
||||
{
|
||||
return oldValidationResult;
|
||||
}
|
||||
if (movedOld != movedTarget || oldEnumerator.Current != targetEnumerator.Current)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool IsValid(this ValidationResult? result)
|
||||
{
|
||||
return result == null || result == ValidationResult.Success;
|
||||
}
|
||||
|
||||
public static IEnumerable<Exception> UnpackException(Exception? exception)
|
||||
{
|
||||
if (exception != null)
|
||||
{
|
||||
var exceptions = exception is AggregateException aggregate ?
|
||||
aggregate.InnerExceptions :
|
||||
(IEnumerable<Exception>)new[] { exception };
|
||||
|
||||
return exceptions.Where(x => !(x is BindingChainException)).ToList();
|
||||
}
|
||||
|
||||
return Array.Empty<Exception>();
|
||||
}
|
||||
|
||||
public static object? UnpackDataValidationException(Exception? exception)
|
||||
{
|
||||
if (exception is DataValidationException dataValidationException)
|
||||
{
|
||||
return dataValidationException.ErrorData;
|
||||
}
|
||||
|
||||
return exception;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the collection contains an equivalent ValidationResult
|
||||
/// </summary>
|
||||
/// <param name="collection">ValidationResults to search through</param>
|
||||
/// <param name="target">ValidationResult to search for</param>
|
||||
/// <returns></returns>
|
||||
public static bool ContainsEqualValidationResult(this ICollection<ValidationResult> collection, ValidationResult target)
|
||||
{
|
||||
return collection.FindEqualValidationResult(target) != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new ValidationResult to the collection if an equivalent does not exist.
|
||||
/// </summary>
|
||||
/// <param name="collection">ValidationResults to search through</param>
|
||||
/// <param name="value">ValidationResult to add</param>
|
||||
public static void AddIfNew(this ICollection<ValidationResult> collection, ValidationResult value)
|
||||
{
|
||||
if (!collection.ContainsEqualValidationResult(value))
|
||||
{
|
||||
collection.Add(value);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ExceptionsMatch(Exception e1, Exception e2)
|
||||
{
|
||||
return e1.Message == e2.Message;
|
||||
}
|
||||
public static void AddExceptionIfNew(this ICollection<Exception> collection, Exception value)
|
||||
{
|
||||
if(!collection.Any(e => ExceptionsMatch(e, value)))
|
||||
{
|
||||
collection.Add(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs an action and catches any non-critical exceptions.
|
||||
/// </summary>
|
||||
/// <param name="action">Action to perform</param>
|
||||
public static void CatchNonCriticalExceptions(Action action)
|
||||
{
|
||||
try
|
||||
{
|
||||
action();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
if (IsCriticalException(exception))
|
||||
{
|
||||
throw;
|
||||
}
|
||||
// Catch any non-critical exceptions
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the specified exception is un-recoverable.
|
||||
/// </summary>
|
||||
/// <param name="exception">The exception.</param>
|
||||
/// <returns>True if the process cannot be recovered from the exception.</returns>
|
||||
public static bool IsCriticalException(Exception exception)
|
||||
{
|
||||
return (exception is OutOfMemoryException) ||
|
||||
(exception is StackOverflowException) ||
|
||||
(exception is AccessViolationException) ||
|
||||
(exception is ThreadAbortException);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user