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

Adding SearchBox to Gallery #662

Open
wants to merge 2 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
12 changes: 12 additions & 0 deletions Sample Applications/WPFGallery/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@
KeyUp="SearchBox_KeyUp"
LostFocus="SearchBox_LostFocus" />-->

<ComboBox
x:Name="SearchBox"
IsEditable="True"
Text="{Binding ViewModel.SearchBoxText, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding ViewModel.FilteredSearchItems}"
DisplayMemberPath="Title"
Margin="10"
PreviewKeyDown="SearchBox_PreviewKeyDown"
GotFocus="SearchBox_GotFocus"
DropDownClosed="SearchBox_DropDownClosed"
IsTextSearchEnabled="False" />


<TreeView
x:Name="ControlsList"
Expand Down
29 changes: 29 additions & 0 deletions Sample Applications/WPFGallery/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,33 @@ private void ControlsList_Loaded(object sender, RoutedEventArgs e)
}
}
}

private void SearchBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
var comboBox = sender as ComboBox;

if(!string.IsNullOrWhiteSpace(comboBox.Text))
{
comboBox.IsDropDownOpen = true;
}
}

private void SearchBox_GotFocus(object sender, RoutedEventArgs e)
{
var comboBox = sender as ComboBox;

comboBox.IsDropDownOpen = true;
}

private void SearchBox_DropDownClosed(object sender, EventArgs e)
{
var comboBox = sender as ComboBox;

if (comboBox.SelectedItem != null)
{
Type currentPageType = ((ControlInfoDataItem)comboBox.SelectedItem).PageType;

_navigationService.Navigate(currentPageType);
}
}
}
37 changes: 37 additions & 0 deletions Sample Applications/WPFGallery/Models/ControlsInfoDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public sealed class ControlsInfoDataSource

private static readonly ControlsInfoDataSource _instance;

private ICollection<ControlInfoDataItem> _allPages;

public static ControlsInfoDataSource Instance
{
get
Expand All @@ -70,6 +72,8 @@ private ControlsInfoDataSource()
{
var jsonText = ReadControlsData();
ControlsInfo = JsonSerializer.Deserialize<List<ControlInfoDataItem>>(jsonText);

GetAllPages();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
GetAllPages();
_allPages = GetAllPages();

}

#endregion
Expand Down Expand Up @@ -111,6 +115,39 @@ public ICollection<ControlInfoDataItem> GetAllControlsInfo()
return allControls;
}

private void GetAllPages()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private void GetAllPages()
public ICollection<ControlInfoDataItem> GetAllPages()

{
_allPages = new ObservableCollection<ControlInfoDataItem>();

foreach (ControlInfoDataItem groupPage in ControlsInfo)
{
_allPages.Add(groupPage);

foreach (ControlInfoDataItem controlPage in groupPage.Items)
{
_allPages.Add(controlPage);
}
}
}

public ObservableCollection<ControlInfoDataItem> FilterItems(string filterText)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not return ICollection<ControlInfoDataItem> from here ?

{
ObservableCollection<ControlInfoDataItem> filteredItems = new();

if (string.IsNullOrEmpty(filterText))
{
filteredItems = new ObservableCollection<ControlInfoDataItem>(_allPages);
}
else
{
filteredItems = new ObservableCollection<ControlInfoDataItem>(
_allPages.Where(item => item.Title.ToLower().Contains(filterText.ToLower()))
);
}

return filteredItems;
}

public ICollection<ControlInfoDataItem> GetGroupedControlsInfo()
{
return ControlsInfo.Where(x => x.IsGroup == true && x.UniqueId != "Design Guidance" && x.UniqueId != "Samples").ToList();
Expand Down
41 changes: 41 additions & 0 deletions Sample Applications/WPFGallery/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ public partial class MainWindowViewModel : ObservableObject

[ObservableProperty]
private ICollection<ControlInfoDataItem> _controls;

[ObservableProperty]
private ICollection<ControlInfoDataItem> _allPages;

public ObservableCollection<ControlInfoDataItem> FilteredSearchItems { get; set; }

private string _searchBoxText;

public string SearchBoxText
{
get => _searchBoxText;
set
{
_searchBoxText = value;
OnPropertyChanged(nameof(SearchBoxText));

FilterItems();
}
}

[ObservableProperty]
private ControlInfoDataItem? _selectedControl;
private readonly INavigationService _navigationService;
Expand Down Expand Up @@ -51,6 +71,20 @@ public void Forward()
public MainWindowViewModel(INavigationService navigationService)
{
_controls = ControlsInfoDataSource.Instance.ControlsInfo;

_allPages = new ObservableCollection<ControlInfoDataItem>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just access the GetAllPages() function from ControlsInfoDataSource for this.

foreach (ControlInfoDataItem groupControl in _controls)
{
_allPages.Add(groupControl);

foreach(ControlInfoDataItem control in groupControl.Items)
{
_allPages.Add(control);
}
}

FilteredSearchItems = new ObservableCollection<ControlInfoDataItem>(AllPages);

_navigationService = navigationService;

_timer = new DispatcherTimer();
Expand All @@ -65,6 +99,13 @@ public void UpdateSearchText(string searchText)
_timer.Start();
}

private void FilterItems()
{
FilteredSearchItems = ControlsInfoDataSource.Instance.FilterItems(SearchBoxText);

OnPropertyChanged(nameof(FilteredSearchItems));
}

private void PerformSearchNavigation(object? sender, EventArgs e)
{
_timer.Stop();
Expand Down
Loading