mirror of
https://github.com/AtomUI/AtomUI.git
synced 2026-10-27 21:58:05 +08:00
feat(SearchEdit): 添加图标搜索按钮点击事件SearchButtonClick
This commit is contained in:
@@ -3,6 +3,7 @@ using AtomUI.Theme.Data;
|
||||
using AtomUI.Theme.Styling;
|
||||
using Avalonia;
|
||||
using Avalonia.Data;
|
||||
using Avalonia.Interactivity;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
@@ -36,6 +37,21 @@ public class SearchEdit : LineEdit
|
||||
|
||||
#endregion
|
||||
|
||||
#region 公共事件定义
|
||||
|
||||
|
||||
public static readonly RoutedEvent<RoutedEventArgs> SearchButtonClickEvent =
|
||||
RoutedEvent.Register<SearchEdit, RoutedEventArgs>(nameof(SearchButtonClick), RoutingStrategies.Bubble);
|
||||
|
||||
|
||||
public event EventHandler<RoutedEventArgs>? SearchButtonClick
|
||||
{
|
||||
add => AddHandler(SearchButtonClickEvent, value);
|
||||
remove => RemoveHandler(SearchButtonClickEvent, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
@@ -44,4 +60,10 @@ public class SearchEdit : LineEdit
|
||||
BindingPriority.Template,
|
||||
new RenderScaleAwareThicknessConfigure(this)));
|
||||
}
|
||||
|
||||
internal void OnSearchButtonClick(RoutedEventArgs e)
|
||||
{
|
||||
var eventArgs = new RoutedEventArgs(SearchButtonClickEvent, this);
|
||||
RaiseEvent(eventArgs);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ using AtomUI.Data;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.LogicalTree;
|
||||
|
||||
namespace AtomUI.Controls;
|
||||
|
||||
@@ -32,6 +34,7 @@ public class SearchEditDecoratedBox : AddOnDecoratedBox
|
||||
|
||||
private Rect? _originRect;
|
||||
private Button? _searchButton;
|
||||
private SearchEdit? _ownerSearchEdit;
|
||||
|
||||
protected override void NotifyAddOnBorderInfoCalculated()
|
||||
{
|
||||
@@ -57,12 +60,42 @@ public class SearchEditDecoratedBox : AddOnDecoratedBox
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
|
||||
if (_searchButton is not null)
|
||||
{
|
||||
_searchButton.Click -= HandleSearchButtonClick;
|
||||
}
|
||||
|
||||
_searchButton = e.NameScope.Find<Button>(AddOnDecoratedBoxThemeConstants.RightAddOnPart);
|
||||
|
||||
if (_searchButton is not null)
|
||||
{
|
||||
BindUtils.RelayBind(this, RightAddOnBorderThicknessProperty, _searchButton, BorderThicknessProperty);
|
||||
BindUtils.RelayBind(this, RightAddOnCornerRadiusProperty, _searchButton, CornerRadiusProperty);
|
||||
_searchButton.Click += HandleSearchButtonClick;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
_ownerSearchEdit = this.FindLogicalAncestorOfType<SearchEdit>();
|
||||
}
|
||||
|
||||
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnDetachedFromVisualTree(e);
|
||||
|
||||
if (_searchButton is not null)
|
||||
{
|
||||
_searchButton.Click -= HandleSearchButtonClick;
|
||||
}
|
||||
|
||||
_ownerSearchEdit = null;
|
||||
}
|
||||
|
||||
private void HandleSearchButtonClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
_ownerSearchEdit?.OnSearchButtonClick(e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user