Beispiel #1
0
        // Seach tool by user input
        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            if (SearchBox.Text != "Search Tool..." && SearchBox.Text.Length > 0)
            {
                // Check if only numbers
                bool digitsOnly = SearchBox.Text.All(char.IsDigit);

                // Load data from SQLite
                var input = SqliteDataAcces.LoadAllToolData();

                // Create new collection
                var searchData = new ObservableCollection <ToolModel>();

                // Apply filter for every item
                foreach (var item in input)
                {
                    if (digitsOnly)
                    {
                        if (item.ToolID.ToString().Contains(SearchBox.Text))
                        {
                            searchData.Add(item);
                        }
                    }
                    else
                    {
                        string[] searchValues = SearchBox.Text.ToLower().Split(null);
                        int      hitCounter   = 0;

                        foreach (string value in searchValues)
                        {
                            if (item.Description.ToLower().Contains(value))
                            {
                                hitCounter += 1;
                            }

                            if (hitCounter == searchValues.Length)
                            {
                                searchData.Add(item);
                            }
                        }
                    }
                }
                //Display data
                ToolDataGrid.DataContext = searchData;
            }
        }