Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making the SearchBox an Editable ComboBox instead of TextBox #559

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Sample Applications/WPFGallery/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,17 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<TextBox x:Name="SearchBox"
<!--<TextBox x:Name="SearchBox"
AutomationProperties.Name="Search"
Margin="10" Width="250"
KeyUp="SearchBox_KeyUp"
LostFocus="SearchBox_LostFocus" />
LostFocus="SearchBox_LostFocus" />-->

<TextBox x:Name="PlaceholderText" Text="Search" Margin="10"/>

<ComboBox x:Name="SearchComboBox" Margin="10" MinWidth="250" HorizontalAlignment="Left" StaysOpenOnEdit="True"
SelectionChanged="ComboBox_SelectionChanged" IsEditable="True" TextBoxBase.TextChanged="SearchComboBox_TextChanged"
GotFocus="SearchComboBox_GotFocus" LostFocus="SearchComboBox_LostFocus" IsTextSearchEnabled="False"/>

<TreeView x:Name="ControlsList"
AutomationProperties.Name="Navigation Pane"
Expand Down
49 changes: 47 additions & 2 deletions Sample Applications/WPFGallery/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public MainWindow(MainWindowViewModel viewModel, IServiceProvider serviceProvide
);

this.StateChanged += MainWindow_StateChanged;
SearchComboBox.ItemsSource = ViewModel.NavigationItemsForSearchBox;
}

private void MainWindow_StateChanged(object sender, EventArgs e)
Expand Down Expand Up @@ -107,7 +108,7 @@ private void Toggle_TitleButtonVisibility()
}
}

private void SearchBox_KeyUp(object sender, KeyEventArgs e)
/*private void SearchBox_KeyUp(object sender, KeyEventArgs e)
{
ViewModel.UpdateSearchText(SearchBox.Text);
}
Expand All @@ -116,7 +117,7 @@ private void SearchBox_LostFocus(object sender, RoutedEventArgs e)
{
SearchBox.Text = "";
ViewModel.UpdateSearchText(SearchBox.Text);
}
}*/

private void MinimizeWindow(object sender, RoutedEventArgs e)
{
Expand Down Expand Up @@ -168,4 +169,48 @@ private void OnNavigating(object? sender, NavigatingEventArgs e)
}
}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
NavigationItem selectedItem = SearchComboBox.SelectedItem as NavigationItem;

if (selectedItem is not null)
{
_navigationService.NavigateTo(selectedItem.PageType);
var tvi = ControlsList.ItemContainerGenerator.ContainerFromItem(selectedItem) as TreeViewItem;
if (tvi != null)
{
tvi.IsExpanded = true;
tvi.BringIntoView();
}

SearchComboBox.ItemsSource = ViewModel.NavigationItemsForSearchBox;
SearchComboBox.SelectedItem = null;
}

SearchComboBox.IsDropDownOpen = false;
PlaceholderText.Visibility = Visibility.Visible;
Keyboard.ClearFocus();
}

private void SearchComboBox_TextChanged(object sender, TextChangedEventArgs e)
{
Task.Delay(300);
string searchText = (sender as ComboBox).Text;
SearchComboBox.IsDropDownOpen = true;
SearchComboBox.ItemsSource = ViewModel.NavigationItemsForSearchBox.Where(item => item.Name.Contains(searchText));
}

private void SearchComboBox_GotFocus(object sender, RoutedEventArgs e)
{
PlaceholderText.Visibility = Visibility.Hidden;
SearchComboBox.IsDropDownOpen = true;
SearchComboBox.ItemsSource = ViewModel.NavigationItemsForSearchBox;
}

private void SearchComboBox_LostFocus(object sender, RoutedEventArgs e)
{
SearchComboBox.ItemsSource = ViewModel.NavigationItemsForSearchBox;
SearchComboBox.IsDropDownOpen = false;
PlaceholderText.Visibility = Visibility.Visible;
}
}
15 changes: 15 additions & 0 deletions Sample Applications/WPFGallery/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ public partial class MainWindowViewModel : ObservableObject
private NavigationItem? _selectedControl;
private INavigationService _navigationService;

[ObservableProperty]
private ICollection<NavigationItem> _navigationItemsForSearchBox = new ObservableCollection<NavigationItem>();

[RelayCommand]
public void Settings()
{
Expand All @@ -192,13 +195,25 @@ public void Forward()
{
_navigationService.NavigateForward();
}
private void GetAllTheNavigationItems()
{
foreach (var control in Controls)
{
NavigationItemsForSearchBox.Add((NavigationItem)control);
foreach (var child in control.Children)
{
NavigationItemsForSearchBox.Add((NavigationItem)child);
}
}
}

public MainWindowViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(400);
_timer.Tick += PerformSearchNavigation;
GetAllTheNavigationItems();
}

public void UpdateSearchText(string searchText)
Expand Down
Loading