Compare commits

..

1 Commits

Author SHA1 Message Date
Leilei Zhang
3cde0b2a68 fix using wrong event 2025-08-06 09:43:02 +08:00
4 changed files with 3 additions and 55 deletions

View File

@@ -8,7 +8,6 @@
xmlns:cpcontrols="using:Microsoft.CmdPal.UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="RootSearchBar"
mc:Ignorable="d">
<UserControl.Resources>

View File

@@ -2,8 +2,6 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.WinUI;
using Microsoft.CmdPal.Core.ViewModels;
@@ -38,9 +36,6 @@ public sealed partial class SearchBar : UserControl,
private string? _lastText;
private string? _deletedSuggestion;
// An ICommand for clearing the search box, allowing the DeleteButton to invoke this logic via MVVM command binding.
public ICommand ClearSearchCommand { get; }
public PageViewModel? CurrentPageViewModel
{
get => (PageViewModel?)GetValue(CurrentPageViewModelProperty);
@@ -80,11 +75,6 @@ public sealed partial class SearchBar : UserControl,
WeakReferenceMessenger.Default.Register<GoHomeMessage>(this);
WeakReferenceMessenger.Default.Register<FocusSearchBoxMessage>(this);
WeakReferenceMessenger.Default.Register<UpdateSuggestionMessage>(this);
// Attach a keydown event handler Clear Button within the FilterBox to trigger search clearing logic.
FilterBox.AddHandler(Button.KeyDownEvent, new KeyEventHandler(DeleteButton_KeyDown), true);
ClearSearchCommand = new RelayCommand(() => ExecuteDeleteButtonAction());
}
public void ClearSearch()
@@ -102,36 +92,6 @@ public sealed partial class SearchBar : UserControl,
}));
}
private Button? GetDeleteButton()
{
// Try to find the DeleteButton in the FilterBox's template
return FilterBox?.FindName("DeleteButton") as Button;
}
private bool IsDeleteButtonFocused()
{
var deleteButton = GetDeleteButton();
var focusedElement = FocusManager.GetFocusedElement() as Button;
return deleteButton != null && focusedElement == deleteButton;
}
public void DeleteButton_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (IsDeleteButtonFocused())
{
if (e.Key == VirtualKey.Enter)
{
ExecuteDeleteButtonAction();
}
}
}
private void ExecuteDeleteButtonAction()
{
ClearSearch();
FilterBox.Focus(Microsoft.UI.Xaml.FocusState.Programmatic);
}
public void SelectSearch()
{
// TODO GH #239 switch back when using the new MD text block

View File

@@ -434,7 +434,4 @@ Right-click to remove the key combination, thereby deactivating the shortcut.</v
<data name="StatusMessagesButton.[using:Microsoft.UI.Xaml.Controls]ToolTipService.ToolTip" xml:space="preserve">
<value>Show status messages</value>
</data>
<data name="DeleteButton.AutomationProperties.Name" xml:space="preserve">
<value>Clears the search box</value>
</data>
</root>

View File

@@ -14,11 +14,6 @@
EmptyValue="Visible"
NotEmptyValue="Collapsed" />
<converters:StringVisibilityConverter
x:Key="StringVisibilityConverter"
EmptyValue="Collapsed"
NotEmptyValue="Visible" />
<Style x:Key="SearchTextBoxStyle" TargetType="TextBox">
<Setter Property="Foreground" Value="{ThemeResource TextControlForeground}" />
<Setter Property="Background" Value="Transparent" />
@@ -171,21 +166,18 @@
Visibility="{Binding Description, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource ReverseStringVisibilityConverter}, Mode=OneWay}" />
<Button
x:Name="DeleteButton"
x:Uid="DeleteButton"
Grid.Column="2"
Margin="0,0,8,0"
Padding="4"
HorizontalAlignment="Right"
VerticalAlignment="Center"
AutomationProperties.AccessibilityView="Content"
AutomationProperties.AccessibilityView="Raw"
BorderThickness="{TemplateBinding BorderThickness}"
Command="{Binding ClearSearchCommand, ElementName=RootSearchBar}"
CornerRadius="{TemplateBinding CornerRadius}"
FontSize="{TemplateBinding FontSize}"
IsTabStop="True"
KeyDown="DeleteButton_KeyDown"
IsTabStop="False"
Style="{StaticResource DeleteButtonStyle}"
Visibility="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource StringVisibilityConverter}, Mode=OneWay}" />
Visibility="Collapsed" />
<TextBlock
x:Name="DescriptionPresenter"
Grid.Column="1"