Загрузка данных


using zadanie2.Models;

namespace zadanie2
{
    public partial class MainWindow : Window
    {
        private ObservableCollection<Recipe> recipes;
        private int nextId = 1;

        public MainWindow()
        {
            InitializeComponent();

            recipes = new ObservableCollection<Recipe>();

            recipes.Add(new Recipe
            {
                Id = nextId++,
                Title = "Борщ",
                Category = "Супы",
                CookingTime = 60,
                Ingredients = "Свекла\nКартофель\nМясо",
                Instructions = "Сварить ингредиенты",
                AddedDate = System.DateTime.Today
            });

            lstRecipes.ItemsSource = recipes;
        }

        private void AddRecipe_Click(object sender, RoutedEventArgs e)
        {
            AddRecipeWindow window = new AddRecipeWindow();

            if (window.ShowDialog() == true)
            {
                window.NewRecipe.Id = nextId++;
                recipes.Add(window.NewRecipe);
                ApplyFilter();
            }
        }

        private void ShowRecipe_Click(object sender, RoutedEventArgs e)
        {
            Recipe recipe = lstRecipes.SelectedItem as Recipe;

            if (recipe == null)
            {
                MessageBox.Show("Выберите рецепт.");
                return;
            }

            RecipeDetailsWindow details =
                new RecipeDetailsWindow(recipe);

            details.ShowDialog();
        }

        private void DeleteRecipe_Click(object sender, RoutedEventArgs e)
        {
            Recipe recipe = lstRecipes.SelectedItem as Recipe;

            if (recipe == null)
                return;

            recipes.Remove(recipe);
            ApplyFilter();
        }

        private void FilterChanged(object sender, RoutedEventArgs e)
        {
            ApplyFilter();
        }

        private void ApplyFilter()
        {
            string search =
                txtSearch.Text.ToLower();

            string category =
                ((ComboBoxItem)cmbCategory.SelectedItem)
                .Content.ToString();

            var result = recipes.Where(r =>
                r.Title.ToLower().Contains(search) &&
                (category == "Все" ||
                 r.Category == category))
                .ToList();

            lstRecipes.ItemsSource = result;
        }
    }
}