Ver Fonte

Added PointerPressed and PointerReleased events to DataGrid header (#12595)

* Added PointerPressed and PointerReleased events to the header in a DataGridColumn

* update refs

* Update Avalonia.Controls.DataGrid.csproj

* revert project file changes

* more reversion

* removed spaces

---------

Co-authored-by: Max Katz <[email protected]>
Co-authored-by: Tim <[email protected]>
Chris Tacke há 2 anos atrás
pai
commit
53c646be06

+ 20 - 8
src/Avalonia.Controls.DataGrid/DataGridColumn.cs

@@ -3,19 +3,20 @@
 // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 // All other rights reserved.
 
-using Avalonia.Data;
-using Avalonia.Interactivity;
-using Avalonia.VisualTree;
-using Avalonia.Collections;
 using System;
 using System.ComponentModel;
-using System.Linq;
 using System.Diagnostics;
+using System.Linq;
+using Avalonia.Collections;
 using Avalonia.Controls.Templates;
 using Avalonia.Controls.Utils;
+using Avalonia.Data;
+using Avalonia.Input;
+using Avalonia.Interactivity;
 using Avalonia.Layout;
 using Avalonia.Markup.Xaml.MarkupExtensions;
 using Avalonia.Styling;
+using Avalonia.VisualTree;
 
 namespace Avalonia.Controls
 {
@@ -38,6 +39,15 @@ namespace Avalonia.Controls
         private Classes _cellStyleClasses;
         private bool _setWidthInternalNoCallback;
 
+        /// <summary>
+        /// Occurs when the pointer is pressed over the column's header
+        /// </summary>
+        public event EventHandler<PointerPressedEventArgs> HeaderPointerPressed;
+        /// <summary>
+        /// Occurs when the pointer is released over the column's header
+        /// </summary>
+        public event EventHandler<PointerReleasedEventArgs> HeaderPointerReleased;
+
         /// <summary>
         /// Initializes a new instance of the <see cref="T:Avalonia.Controls.DataGridColumn" /> class.
         /// </summary>
@@ -331,7 +341,7 @@ namespace Avalonia.Controls
                     }
 
                     // return whether or not the property type can be compared
-                    return (typeof(IComparable).IsAssignableFrom(propertyType)) ? true : false;
+                    return typeof(IComparable).IsAssignableFrom(propertyType) ? true : false;
                 }
                 else
                 {
@@ -810,7 +820,7 @@ namespace Avalonia.Controls
         /// <param name="source"></param>
         /// <param name="width">The DataGridLength to coerce.</param>
         /// <returns>The resultant (coerced) DataGridLength.</returns>
-        static DataGridLength CoerceWidth(AvaloniaObject source, DataGridLength width)
+        private static DataGridLength CoerceWidth(AvaloniaObject source, DataGridLength width)
         {
             var target = (DataGridColumn)source;
 
@@ -911,6 +921,8 @@ namespace Avalonia.Controls
                 result.SetValue(StyledElement.ThemeProperty, columnTheme, BindingPriority.Template);
             }
 
+            result.PointerPressed += (s, e) => { HeaderPointerPressed?.Invoke(this, e); };
+            result.PointerReleased += (s, e) => { HeaderPointerReleased?.Invoke(this, e); };
             return result;
         }
 
@@ -1008,7 +1020,7 @@ namespace Avalonia.Controls
                 {
                     // Recalculate star weight of this column based on the new desired value
                     InheritsWidth = false;
-                    newValue = (Width.Value * newDisplayValue) / ActualWidth;
+                    newValue = Width.Value * newDisplayValue / ActualWidth;
                 }
             }
 

+ 14 - 11
src/Avalonia.Controls.DataGrid/DataGridColumnHeader.cs

@@ -3,18 +3,17 @@
 // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 // All other rights reserved.
 
-using Avalonia.Controls.Primitives;
+using System;
+using System.ComponentModel;
+using System.Diagnostics;
+using Avalonia.Collections;
+using Avalonia.Controls.Metadata;
+using Avalonia.Controls.Mixins;
+using Avalonia.Controls.Utils;
 using Avalonia.Data;
 using Avalonia.Input;
-using Avalonia.Collections;
 using Avalonia.Media;
-using System.ComponentModel;
-using System.Diagnostics;
 using Avalonia.Utilities;
-using System;
-using Avalonia.Controls.Utils;
-using Avalonia.Controls.Mixins;
-using Avalonia.Controls.Metadata;
 
 namespace Avalonia.Controls
 {
@@ -195,8 +194,12 @@ namespace Avalonia.Controls
             }
         }
 
+        public event EventHandler<KeyModifiers> LeftClick;
+
         internal void OnMouseLeftButtonUp_Click(KeyModifiers keyModifiers, ref bool handled)
         {
+            LeftClick?.Invoke(this, keyModifiers);
+
             // completed a click without dragging, so we're sorting
             InvokeProcessSort(keyModifiers);
             handled = true;
@@ -441,7 +444,7 @@ namespace Avalonia.Controls
             }
 
             Debug.Assert(OwningGrid.Parent is InputElement);
-            
+
             OnMouseMove_Resize(ref handled, mousePositionHeaders);
 
             OnMouseMove_Reorder(ref handled, mousePosition, mousePositionHeaders);
@@ -669,7 +672,7 @@ namespace Avalonia.Controls
                 Content = Content,
                 ContentTemplate = ContentTemplate
             };
-            if (OwningGrid.ColumnHeaderTheme is {} columnHeaderTheme)
+            if (OwningGrid.ColumnHeaderTheme is { } columnHeaderTheme)
             {
                 dragIndicator.SetValue(ThemeProperty, columnHeaderTheme, BindingPriority.Template);
             }
@@ -786,7 +789,7 @@ namespace Avalonia.Controls
 
                 desiredWidth = Math.Max(_dragColumn.ActualMinWidth, Math.Min(_dragColumn.ActualMaxWidth, desiredWidth));
                 _dragColumn.Resize(_dragColumn.Width,
-                    new(_dragColumn.Width.Value, _dragColumn.Width.UnitType, _dragColumn.Width.DesiredValue, desiredWidth), 
+                    new(_dragColumn.Width.Value, _dragColumn.Width.UnitType, _dragColumn.Width.DesiredValue, desiredWidth),
                     true);
 
                 OwningGrid.UpdateHorizontalOffset(_originalHorizontalOffset);