From 7c9172cd0f6ab7d0286e83405a44edcae406a647 Mon Sep 17 00:00:00 2001 From: Harshit Mishra Date: Tue, 1 Oct 2024 18:34:42 +0530 Subject: [PATCH 1/4] Basic search box without navigation and pages --- .../WPFGallery/MainWindow.xaml | 7 +++ .../WPFGallery/MainWindow.xaml.cs | 10 ++++ .../ViewModels/MainWindowViewModel.cs | 46 +++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/Sample Applications/WPFGallery/MainWindow.xaml b/Sample Applications/WPFGallery/MainWindow.xaml index 5d0cf364..2959ba20 100644 --- a/Sample Applications/WPFGallery/MainWindow.xaml +++ b/Sample Applications/WPFGallery/MainWindow.xaml @@ -210,6 +210,13 @@ KeyUp="SearchBox_KeyUp" LostFocus="SearchBox_LostFocus" />--> + FilteredSearchItems { get; set; } + public ObservableCollection SearchItems { get; set; } + + private string _searchBoxText; + + public string SearchBoxText + { + get => _searchBoxText; + set + { + _searchBoxText = value; + OnPropertyChanged(nameof(SearchBoxText)); + FilterItems(); + } + } + + private void FilterItems() + { + if (string.IsNullOrEmpty(SearchBoxText)) + { + FilteredSearchItems = new ObservableCollection(SearchItems); + } + else + { + FilteredSearchItems = new ObservableCollection( + SearchItems.Where(item => item.ToLower().Contains(SearchBoxText.ToLower())) + ); + } + + OnPropertyChanged(nameof(FilteredSearchItems)); + } + private readonly DispatcherTimer _timer; private string _searchText = string.Empty; @@ -56,6 +88,20 @@ public MainWindowViewModel(INavigationService navigationService) _timer = new DispatcherTimer(); _timer.Interval = TimeSpan.FromMilliseconds(400); _timer.Tick += PerformSearchNavigation; + + SearchItems = new ObservableCollection + { + "Apple", + "Banana", + "Cherry", + "Cheese", + "Choix", + "Date", + "Dragon Fruit", + "Dairy" + }; + + FilteredSearchItems = new ObservableCollection(SearchItems); } public void UpdateSearchText(string searchText) From 191a318fcb4b2a1a2cdbc0815ac7625605b01ce0 Mon Sep 17 00:00:00 2001 From: Harshit Mishra Date: Thu, 3 Oct 2024 20:31:34 +0530 Subject: [PATCH 2/4] Raw searchbox implementation --- .../WPFGallery/MainWindow.xaml | 8 ++- .../WPFGallery/MainWindow.xaml.cs | 28 +++++---- .../Navigation/NavigationService.cs | 59 +++++++++++++++++++ .../ViewModels/MainWindowViewModel.cs | 21 +++---- 4 files changed, 93 insertions(+), 23 deletions(-) diff --git a/Sample Applications/WPFGallery/MainWindow.xaml b/Sample Applications/WPFGallery/MainWindow.xaml index 2959ba20..109e41ce 100644 --- a/Sample Applications/WPFGallery/MainWindow.xaml +++ b/Sample Applications/WPFGallery/MainWindow.xaml @@ -210,13 +210,17 @@ KeyUp="SearchBox_KeyUp" LostFocus="SearchBox_LostFocus" />--> + + + PreviewKeyDown="ComboBox_PreviewKeyDown" + GotFocus="SearchBox_GotFocus" + DropDownClosed="ComboBox_DropDownClosed" /> Navigating; + private Dictionary pageNameToTypeMapping; + public NavigationService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; _history = new Stack(); _future = new Stack(); + + initializeMapping(); + } + + private void initializeMapping() + { + pageNameToTypeMapping = new Dictionary() + { + { "home", typeof(DashboardPage) }, + { "design guidance", typeof(DesignGuidancePage) }, + { "colors", typeof(ColorsPage) }, + { "typography", typeof(TypographyPage) }, + { "icons", typeof(IconsPage) }, + { "samples", typeof(SamplesPage) }, + { "user dashboard", typeof(UserDashboardPage) }, + { "all controls", typeof(AllSamplesPage) }, + { "basic input", typeof(BasicInputPage) }, + { "button", typeof(ButtonPage) }, + { "checkbox", typeof(CheckBoxPage) }, + { "radiobutton", typeof(RadioButtonPage) }, + { "combobox", typeof(ComboBoxPage) }, + { "slider", typeof(SliderPage) }, + { "date & calendar", typeof(DateAndTimePage) }, + { "calendar", typeof(CalendarPage) }, + { "datepicker", typeof(DatePickerPage) }, + { "layout", typeof(LayoutPage) }, + { "expander", typeof(ExpanderPage) }, + { "navigation", typeof(NavigationPage) }, + { "menu", typeof(MenuPage) }, + { "tabcontrol", typeof(TabControlPage) }, + { "collections", typeof(CollectionsPage) }, + { "datagrid", typeof(DataGridPage) }, + { "listbox", typeof(ListBoxPage) }, + { "listview", typeof(ListViewPage) }, + { "treeview", typeof(TreeViewPage) }, + { "status & info", typeof(StatusAndInfoPage) }, + { "progressbar", typeof(ProgressBarPage) }, + { "tooltip", typeof(ToolTipPage) }, + { "text", typeof(TextPage) }, + { "label", typeof(LabelPage) }, + { "textbox", typeof(TextBoxPage) }, + { "textblock", typeof(TextBlockPage) }, + { "richtextedit", typeof(RichTextEditPage) }, + { "passwordbox", typeof(PasswordBoxPage) } + }; } public void SetFrame(Frame frame) @@ -73,6 +123,15 @@ public void Navigate(Type type) } } + public void Navigate(string? pageName) + { + if(!string.IsNullOrEmpty(pageName) && pageNameToTypeMapping.ContainsKey(pageName.ToLower())) + { + Type currentPage = pageNameToTypeMapping[pageName.ToLower()]; + Navigate(currentPage); + } + } + public void NavigateBack() { if(_history.Count > 0) diff --git a/Sample Applications/WPFGallery/ViewModels/MainWindowViewModel.cs b/Sample Applications/WPFGallery/ViewModels/MainWindowViewModel.cs index c73c6de6..3231f67d 100644 --- a/Sample Applications/WPFGallery/ViewModels/MainWindowViewModel.cs +++ b/Sample Applications/WPFGallery/ViewModels/MainWindowViewModel.cs @@ -89,17 +89,18 @@ public MainWindowViewModel(INavigationService navigationService) _timer.Interval = TimeSpan.FromMilliseconds(400); _timer.Tick += PerformSearchNavigation; - SearchItems = new ObservableCollection + SearchItems = new(); + + foreach (var mainPage in Controls) { - "Apple", - "Banana", - "Cherry", - "Cheese", - "Choix", - "Date", - "Dragon Fruit", - "Dairy" - }; + + SearchItems.Add(mainPage.ToString()); + + foreach (ControlInfoDataItem controlsPage in mainPage.Items) + { + SearchItems.Add(controlsPage.ToString()); + } + } FilteredSearchItems = new ObservableCollection(SearchItems); } From 04d0308366567702f898bd5ce3a567655eedd267 Mon Sep 17 00:00:00 2001 From: Harshit Mishra Date: Thu, 3 Oct 2024 20:39:25 +0530 Subject: [PATCH 3/4] Code Clean-up and Refactoring --- Sample Applications/WPFGallery/MainWindow.xaml | 4 ++-- Sample Applications/WPFGallery/MainWindow.xaml.cs | 4 ++-- .../WPFGallery/ViewModels/MainWindowViewModel.cs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Sample Applications/WPFGallery/MainWindow.xaml b/Sample Applications/WPFGallery/MainWindow.xaml index 109e41ce..2a2b546a 100644 --- a/Sample Applications/WPFGallery/MainWindow.xaml +++ b/Sample Applications/WPFGallery/MainWindow.xaml @@ -218,9 +218,9 @@ ItemsSource="{Binding ViewModel.FilteredSearchItems}" Margin="10" IsTextSearchEnabled="False" - PreviewKeyDown="ComboBox_PreviewKeyDown" + PreviewKeyDown="SearchBox_PreviewKeyDown" GotFocus="SearchBox_GotFocus" - DropDownClosed="ComboBox_DropDownClosed" /> + DropDownClosed="SearchBox_DropDownClosed" /> SearchItems { get; set; } + public ObservableCollection FilteredSearchItems { get; set; } - public ObservableCollection SearchItems { get; set; } private string _searchBoxText; @@ -91,9 +92,8 @@ public MainWindowViewModel(INavigationService navigationService) SearchItems = new(); - foreach (var mainPage in Controls) + foreach (ControlInfoDataItem mainPage in Controls) { - SearchItems.Add(mainPage.ToString()); foreach (ControlInfoDataItem controlsPage in mainPage.Items) From b0a048087b3021f38fbe1d0ceedb59ef994186e6 Mon Sep 17 00:00:00 2001 From: Harshit Mishra Date: Fri, 4 Oct 2024 15:12:38 +0530 Subject: [PATCH 4/4] Addressing PR Comments 1 --- .../WPFGallery/MainWindow.xaml.cs | 7 ++ .../Models/ControlsInfoDataSource.cs | 20 ++++++ .../Navigation/NavigationService.cs | 64 ++++++------------- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/Sample Applications/WPFGallery/MainWindow.xaml.cs b/Sample Applications/WPFGallery/MainWindow.xaml.cs index 542bba57..093bffe5 100644 --- a/Sample Applications/WPFGallery/MainWindow.xaml.cs +++ b/Sample Applications/WPFGallery/MainWindow.xaml.cs @@ -47,6 +47,13 @@ public MainWindow(MainWindowViewModel viewModel, IServiceProvider serviceProvide SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged; this.StateChanged += MainWindow_StateChanged; + + ChangeSearchBoxStyle(); + } + + private void ChangeSearchBoxStyle() + { + var template = SearchBox.Template; } private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) diff --git a/Sample Applications/WPFGallery/Models/ControlsInfoDataSource.cs b/Sample Applications/WPFGallery/Models/ControlsInfoDataSource.cs index 116b08bc..de7fa7fd 100644 --- a/Sample Applications/WPFGallery/Models/ControlsInfoDataSource.cs +++ b/Sample Applications/WPFGallery/Models/ControlsInfoDataSource.cs @@ -115,5 +115,25 @@ public ICollection GetGroupedControlsInfo() { return ControlsInfo.Where(x => x.IsGroup == true && x.UniqueId != "Design Guidance" && x.UniqueId != "Samples").ToList(); } + + public ObservableCollection GetAllPagesForSearch() + { + ObservableCollection allPagesForSearch = new ObservableCollection(); + + ICollection allPages = ControlsInfoDataSource.Instance.ControlsInfo; + + foreach (ControlInfoDataItem item in allPages) + { + allPagesForSearch.Add(item.Title); + + foreach (ControlInfoDataItem individualItem in item.Items) + { + allPagesForSearch.Add(individualItem.Title); + } + } + + return allPagesForSearch; + + } } } diff --git a/Sample Applications/WPFGallery/Navigation/NavigationService.cs b/Sample Applications/WPFGallery/Navigation/NavigationService.cs index a5f8f9eb..8441ccfb 100644 --- a/Sample Applications/WPFGallery/Navigation/NavigationService.cs +++ b/Sample Applications/WPFGallery/Navigation/NavigationService.cs @@ -1,6 +1,4 @@ -using System.Collections; -using System.Windows.Navigation; -using WPFGallery.Views; +using WPFGallery.Models; namespace WPFGallery.Navigation; @@ -43,7 +41,7 @@ public class NavigationService : INavigationService public event EventHandler Navigating; - private Dictionary pageNameToTypeMapping; + private Dictionary _pageNameToTypeMapping; public NavigationService(IServiceProvider serviceProvider) @@ -52,50 +50,24 @@ public NavigationService(IServiceProvider serviceProvider) _history = new Stack(); _future = new Stack(); - initializeMapping(); + InitializeMapping(); } - private void initializeMapping() + private void InitializeMapping() { - pageNameToTypeMapping = new Dictionary() + ICollection allPages = ControlsInfoDataSource.Instance.ControlsInfo; + + _pageNameToTypeMapping = new(); + + foreach (ControlInfoDataItem item in allPages) { - { "home", typeof(DashboardPage) }, - { "design guidance", typeof(DesignGuidancePage) }, - { "colors", typeof(ColorsPage) }, - { "typography", typeof(TypographyPage) }, - { "icons", typeof(IconsPage) }, - { "samples", typeof(SamplesPage) }, - { "user dashboard", typeof(UserDashboardPage) }, - { "all controls", typeof(AllSamplesPage) }, - { "basic input", typeof(BasicInputPage) }, - { "button", typeof(ButtonPage) }, - { "checkbox", typeof(CheckBoxPage) }, - { "radiobutton", typeof(RadioButtonPage) }, - { "combobox", typeof(ComboBoxPage) }, - { "slider", typeof(SliderPage) }, - { "date & calendar", typeof(DateAndTimePage) }, - { "calendar", typeof(CalendarPage) }, - { "datepicker", typeof(DatePickerPage) }, - { "layout", typeof(LayoutPage) }, - { "expander", typeof(ExpanderPage) }, - { "navigation", typeof(NavigationPage) }, - { "menu", typeof(MenuPage) }, - { "tabcontrol", typeof(TabControlPage) }, - { "collections", typeof(CollectionsPage) }, - { "datagrid", typeof(DataGridPage) }, - { "listbox", typeof(ListBoxPage) }, - { "listview", typeof(ListViewPage) }, - { "treeview", typeof(TreeViewPage) }, - { "status & info", typeof(StatusAndInfoPage) }, - { "progressbar", typeof(ProgressBarPage) }, - { "tooltip", typeof(ToolTipPage) }, - { "text", typeof(TextPage) }, - { "label", typeof(LabelPage) }, - { "textbox", typeof(TextBoxPage) }, - { "textblock", typeof(TextBlockPage) }, - { "richtextedit", typeof(RichTextEditPage) }, - { "passwordbox", typeof(PasswordBoxPage) } - }; + _pageNameToTypeMapping[item.Title] = item.PageType; + + foreach (ControlInfoDataItem individualItem in item.Items) + { + _pageNameToTypeMapping[individualItem.Title] = individualItem.PageType; + } + } } public void SetFrame(Frame frame) @@ -125,9 +97,9 @@ public void Navigate(Type type) public void Navigate(string? pageName) { - if(!string.IsNullOrEmpty(pageName) && pageNameToTypeMapping.ContainsKey(pageName.ToLower())) + if (!string.IsNullOrEmpty(pageName) && _pageNameToTypeMapping.ContainsKey(pageName)) { - Type currentPage = pageNameToTypeMapping[pageName.ToLower()]; + Type currentPage = _pageNameToTypeMapping[pageName]; Navigate(currentPage); } }