peon125

Untitled

Jun 13th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Controls;
  4. using Winiarzapp.Core.Data;
  5.  
  6. namespace winiarzapp.UI.Windows.MainWindow.Components
  7. {
  8.     /// <summary>
  9.     /// Lista przepisów.
  10.     /// </summary>
  11.     public partial class RecipeList : UserControl
  12.     {
  13.         IRecipeSource recipeSource;
  14.         StackPanel stackPanel;
  15.         string query;
  16.         //Recipe recipe;
  17.  
  18.         public RecipeList()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         public void Initialize(IRecipeSource recipeSource)
  24.         {
  25.             this.recipeSource = recipeSource;
  26.  
  27.             //TODO: Nasłuchuj i reaguj na zmiany w źródle przepisów.
  28.             //TODO: Nasłuchuj i reaguj na zmiany frazy filtrującej wyniki.
  29.  
  30.             //recipe = FindName("Recipe") as Recipe;
  31.  
  32.             stackPanel = FindName("StackPanel") as StackPanel;
  33.  
  34.             recipeSource.RecipesChanged += IRecipeSource_RecipesChanged;
  35.  
  36.  
  37.             // Dodanie pustych elementów żeby zapełnić miejsce. Do usunięcia.
  38.  
  39.             stackPanel.Children.Add(new ListElement());
  40.             stackPanel.Children.Add(new ListElement());
  41.             stackPanel.Children.Add(new ListElement());
  42.             stackPanel.Children.Add(new ListElement());
  43.         }
  44.  
  45.         private void IRecipeSource_RecipesChanged()
  46.         {
  47.             Filter(query);
  48.             // nie wiedziałem co tutaj wrzucić, więc wrzuciłem ponowne wykonanie wyszukiwania, żeby
  49.             // przeszukać zmienioną bazę przepisów
  50.         }
  51.  
  52.         private void SearchBar_QueryChanged(string query)
  53.         {
  54.             this.query = query;
  55.  
  56.             Filter(query);
  57.         }
  58.  
  59.         /// <summary>
  60.         /// Przebuduj listę by wyświetlać elementy pasujące do podanej frazy.
  61.         /// </summary>
  62.         public void Filter(string query)
  63.         {
  64.             stackPanel.Children.Clear();
  65.  
  66.             foreach (Recipe recipe in recipeSource.Recipes)
  67.             {
  68.                if (recipe.Name.ToLower().Contains(query.ToLower()))
  69.                {
  70.                     ListElement listElement = new ListElement();
  71.                     listElement.RenderRecord(recipe);
  72.                     stackPanel.Children.Add(listElement);
  73.                 }
  74.             }
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment